Notifications
Clear all
[Closed] sort array?
Aug 10, 2015 1:09 pm
If I have an array with objects in it, how would I do to “sort” that array by z position so the object with the smallest z pos is placed first in the array?
selected_obj_ar = (selection as array);
–Outputs:
#($Box:Box002 @ [13.024259,2.593246,16.590538], $Box:Box003 @ [16.902788,2.593246,44.123074], $Box:Box004 @ [26.609926,2.593246,29.887850], $Box:Box005 @ [39.337341,2.593246,43.118164], $Box:Box001 @ [-0.674262,2.593246,27.677219])
and should be after the array been sorted:
#($Box:Box002 @ [13.024259,2.593246,16.590538], $Box:Box001 @ [-0.674262,2.593246,27.677219]), $Box:Box004 @ [26.609926,2.593246,29.887850], $Box:Box005 @ [39.337341,2.593246,43.118164], $Box:Box003 @ [16.902788,2.593246,44.123074])
Example img:
Thansk in advance.
5 Replies
Aug 10, 2015 1:09 pm
Have a look at qSort
https://davewortley.wordpress.com/2013/07/12/lesson-12-qsort/
Aug 10, 2015 1:09 pm
Although that example is too complex…
sel = selection as array
fn compareFN v1 v2 =
(
case of
(
(v1.pos.z < v2.pos.z): -1
(v1.pos.z > v2.pos.z): 1
default: 0
)
)
qsort sel compareFN
sel
2 Replies
How would I do to sort the sub-arrays in an multi-dimensional array by a value in the sub arrays?
Example:
myMultiDimArray = #(#([0,0,1],[0,0,50]),#([0,0,2],[0,0,7]),#([0,0,3],[0,0,20]));
and I want to sort by:
myMultiDimArray[1][2].z
myMultiDimArray[2][2].z
myMultiDimArray[3][2].z
so the end result is:
myMultiDimArray = #(#([0,0,2],[0,0,7]),#([0,0,3],[0,0,20]),#([0,0,1],[0,0,50]));
Thanks in advance.
Aug 10, 2015 1:09 pm
nvm solved it.
myMultiDimArray = #(#([1,0,5],[0,0,50]),#([2,0,5],[0,0,7]),#([3,0,5],[0,0,20]),#([4,0,5],[0,0,30]),#([5,0,5],[0,0,45]),#([6,0,5],[0,0,2]));
fn compareFN v1 v2 x:1 =
(
case of
(
(v1[x][3] < v2[x][3]):-1
(v1[x][3] > v2[x][3]):1
default:0
)
)
qsort myMultiDimArray compareFN x:2