Posts: 1,101
Thanks: 1912 in 54 Posts
7 Achievements
How to effectively sort arrays in Simantics
Typically I have associated object arrays, one holding the identifier and one or more holding properties, like columns in a spreadsheet. For example, people in array 0 and how many apples they have in array 1.
There is 0076 Array Operation, 0C and 0D to sort
one array in ascending or descending order. I can't think of a use case for this because if I sort the properties, they lose their relationship to the other columns. So I can take that someone owns 10 apples, someone else owns 6, and yet another person owns 2, but I can't tell who. Is there a way to instruct this opcode to take in multiple arrays at once? When to make use of this?
Approach 1: Loop through the array with 0076, 0A Set index to next. Start with the highest value 10, pick off any matching elements (10), decrement-- (9), if greater than lowest value (0) go back to step 1. This seems rather inefficient if the range from highest to lowest is large or unknown, with many loops finding nothing.
Approach 2: Pack the property and its initial index into one value. Then sort array 1. The packed index still points to the original person entry in array 0. Let's say 9 bits for the property, and 6 bits for the index. This allows for at most 64 people, having at most 511 apples (or -256 to 255), which is satisfactory for a simple example, but not if we want to list the whole neighborhood containing ~500 people.
Approach 3 ???