Notifications
Clear all

[Closed] Sort multidimensional array?

Any suggestions for sorting a multidimensional array?

I have an array in which 1) the first element of each set is the name of an object, and 2) the second element is a number representing the distance from a particular point.

I want to sort the array using the numbers in the second set, from least to greatest distance. What do I need to do in order to accomplish this?

Also, my goal here is just to find out which of the objects is closest to the point I mentioned earlier. If there is a quicker way to do that, I’d like to know it

2 Replies
fn sortByXMember arr1 arr2 x:1 =
(
	case of
	(
		(arr1[x] < arr2[x]):-1
		(arr1[x] > arr2[x]):1
		default:0
	)
)

arr = #(#("box", 10, 40), #("plane", 50, 50), #("cylinder", 30, 10), #("cone", 60, 20))
qsort arr sortByXMember x:2
arr

That’s a useful multi-purpose QSORT function I wrote. Changing the X parameter you pass in will determine which member you sort by (so in your case, x:1 would sort by name/string, and x:2 would sort by distance).

Thanks! Actually I found another way to do what I needed already, but I will definitely be able to find a use for that script!