Darwin Help

Back to Index

sort

Function sort - sort a list

Calling Sequence  sort(L)
sort(L,orderproc)
Parameters
NameTypeDescription

L list(anything)a list of things to be sorted
orderproc procedurean ordering procedure
Return Type  list(anything)
Synopsis The sort function can order a list (array) containing any type of elements as long as these elements are comparable i.e. the operator <= is applicable and well-defined. When only supplied a list, sort places the elements in ascending order and returns a copy of the list. The ordering it uses is ascending order and for other data structures it is the same order that sets use. In particular, if there are no duplicate elements in the input list, sorting without an orderproc or transforming the list into a set have the same effect. The optional second argument must specify an ordering procedure. This procedure may have a single argument, in which case it is understood to return a value on which to order the records, or may take two arguments, in which case it should return true or false depending on whether the arguments are in the desired order. In both cases the arguments will be the entries of the array to be sorted. Sort does not destroy/change its argument, it returns a new array of (sorted) data. Naturally, sort is most efficient when called with a single argument.
Examples
> a := [521, -923, 1293, 521, -3342];
a := [521, -923, 1293, 521, -3342]
> sort(a);
[-3342, -923, 521, 521, 1293]
> a;
[521, -923, 1293, 521, -3342]
> sort(a, x -> -x);
[1293, 521, 521, -923, -3342]
> neg := proc(a) return(-(abs(a))) end;
neg := proc (a) return(-1*|a|) end
> sort(a, neg);
[-3342, 1293, -923, 521, 521]
> b :=[[z, f], [w, e], [y, d]];
b := [[z, f], [w, e], [y, d]]
> sort(b, b->b[2]);
[[y, d], [w, e], [z, f]]


See also set