Quick Reply
Search this Thread
Scholar
Original Poster
#1 Old 20th Jan 2023 at 5:42 AM
Default 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 ???
Advertisement
Lab Assistant
#2 Old 16th Jul 2023 at 1:42 PM
Hi! I used a third approach with my yearbook mod if you want to have a look at it, as I was solving a similar problem.

I wanted to go through a number of Sims in the neighborhood, assign them a score, and then sort scores highest to lowest to see which nID had the highest score.

I used three arrays, one containing Sims, another containing the 'scores' and then a third array.

So you have array 0 (nIDs), and array 1 (scores). Simply copy array 1 to array 2, and then sort array 2; The highest value (x) will be in position zero, so simply store x in an arbitrary variable; knowing the highest value, you can set index to next x in array 1, instead of having to go through each individually. This way array 0 and 1 maintain their relationship.
Back to top