qsort problem


#1

i have an muti array i want to rearrange them in new muti array , can you help me ?

my code :

mangc = #(#(“Box018”, 5563.78), #(“Box034”, 3070.31), #(“Box035”, 6340.63), #(“Box036”, 9610.94), #(“Box037”, 12881.3))

fn sosanhmang v1 v2 =
(
local d = mangc[v1][2] - mangc[v2][2]

case of
(
(d < 0.): -1
(d > 0.): 1
default: 0
)
)
qSort mangc sosanhmang
mangc


#2

qsort already gives two items to be compared to the function, no need to touch the full array.

Replace

local d = mangc[v1][2] - mangc[v2][2]

with

local d = v1[2] - 2[2]

and it should work.


#3

(
	function SortByXMember arr1 arr2 x:1 =
	(
		case of
		(
			(arr1[x] < arr2[x]):-1
			(arr1[x] > arr2[x]):1
			default:0
		)
	)
	
	mangc = #(#("Box018", 5563.78), #("Box034", 3070.31), #("Box035", 6340.63), #("Box036", 9610.94), #("Box037", 12881.3))

	format "mangc: % 
" 	mangc

	qsort mangc SortByXMember x:2	

	format "mangc: % 
" mangc
		
)


#4

i can do it , thank you very much


#5

Note that since max 2017 Update 1, there’s also a built-in Dictionary type.

Dictionary() -- empty dictionary of type #name
Dictionary (#integer | #name | #string) -- empty dictionary of the specified type
Dictionary {#(key, value)}+ -- one or more two-value arrays
Dictionary {key:value}+ -- one or more explicit key:value pairs
Dictionary {(DataPair key value)} -- one or more DataPair objects
<dictionary>[key] : get/set key value
<dictionary>.keys : Array, read-only