up previous next
sort a list
SortBy(L: LIST, LessThanFunc: FUNCTION)
|
This function sorts the elements of the list in
L
in
increasing order with respect to the comparisons made by
LessThanFunc
;
it overwrites
L
and returns
NULL
.
The comparison function
LessThanFunc
takes two arguments
and returns
True
if the
first argument is less than the second, otherwise it returns
False
.
The sorted list is in increasing order.
Note that to call
SortBy(L,LessThanFunc)
inside a function you
will need to make the name
LessThanFunc
accessible using
TopLevel LessThanFunc;
Note that if both
LessThanFunc(A, B)
and
LessThanFunc(B, A)
return
true
, then
A
and
B
are viewed as being equal.
/**/ Define LessThanLen(S, T) -- define the sorting function
/**/ Return len(S) < len(T);
/**/ EndDefine;
/**/ L := ["bird", "mouse", "cat", "elephant"];
/**/ SortBy(ref L, LessThanLen);
/**/ L;
["cat", "bird", "mouse", "elephant"]
|