sort(a)
Sorts the entries of array a in increasing order.
sort(a,f)
Sorts the entries of array a using the user-defined function f. Function call f(x1,x2) must return -1 if x1
is "smaller" than x2
; it must return 1 if x1
is "bigger" than x2
; it must return 0 otherwise.
See also apply reverse pop push
Example
a = ( 2, 1, 7, 3 )
2 1 7 3
sort( a )
1 2 3 7
Next example uses a user-defined function to sort a set of complex numbers.
f(z1,z2) = {
if(abs(z1)<abs(z2)) return -1;
else if(abs(z1)>abs(z2)) return 1;
return 0;
}
f(z1,z2) = {
if(abs(z1)<abs(z2)) return -1;
else if(abs(z1)>abs(z2)) return 1;
return 0;
}
a=(1-2j, 15+3j, 7+1j)
( (1.0 - 2.0 j) (15.0 + 3.0 j) (7.0 + 1.0 j) )
sort(a,f)
( (1.0 - 2.0 j) (7.0 + 1.0 j) (15.0 + 3.0 j) )