I have an array, say Array = #(0.2, 0.6, 0.1, 0.8 )
index : 1 2 3 4
I need to sort this array but I need somehow to keep a trace of the index by values so :
#(0.1, 0.2, 0.6, 0.8 )
3 1 2 4
How could I do that ? I am quite of stuck with it
Thanks
Bloby
EDIT⌠found a solution with multidimentional arrays⌠first sub array = values, second = index, and simply reordering the index after sorting (thanks google on this one)⌠a bit tricky still for something which appear so âbasicâ. Missing a simple function in maxscript for returning this
[QUOTE=Swordslayer;24318]Multidimensional arrays are not needed if you sort the indices first:
arr = #(0.2, 0.6, 0.1, 0.8)
indices = #{1..arr.count} as array
fn compareByIndex i1 i2 arr: =
if arr[i1] > arr[i2] then 1
else if arr[i1] < arr[i2] then -1
else 0
qSort indices compareByIndex arr:arr
indices --> #(3, 1, 2, 4)
sort arr --> #(0.1, 0.2, 0.6, 0.8)
[/QUOTE]
Arg thanks, I was really hoping being able to avoid the qsort, I really donât get it, the help is so unhelpfull for me.
In your exemple I canât figure out what is the âarr:â
here fn compareByIndex i1 i2 arr: =
and qSort indices compareByIndex arr:arr
Itâs simple when I want to rename the first array arr by Test and then change the code accordingly, it stop working, and I really donât get why. What is this arr ? One is the array the other an argument (?) but what is this argument ? The code seems so much more logical without it (which confuse me even more)
Because there are two arrays there and weâre sorting just one, we have to either hardcode the other in the function or pass it to it somehow - look up keyword function arguments in maxscript reference for further info. If you change the name of the variable to Test, the function call will be qSort indices compareByIndex arr:Test (and arr will be a local variable in the compareByIndex function, used in arr[i1] and so on).
instead of creating an array of arrays, you could create an array of Datapair values. Datapair() is a kind of KeyValue type and rarely used and knownâŚ
I had completely forgot about those datapair !!! I canât try now but when I look in the help : <dataPair>.<value1_name> : Value I see I could name my values (?) So maybe could I use the name to stock my index so they follow well my data when using a simple sorting ? If not why using datapairs instead of multidimentonal array ? Itâs faster ?