up previous next
SortBy(ref L: LIST, LessThanFunc: FUNCTION)
|
This procedure sorts the elements of the list in
L
into
increasing order with respect to the comparisons made by
LessThanFunc
;
it overwrites
L
and returns nothing.
The comparison function
LessThanFunc
takes two arguments
and must return
true
if the first argument is less than the
second, otherwise it must return
false
.
If you want to call
SortBy(ref L,LessThanFunc)
inside a function, and
you have defined
LessThanFunc
at top level (rather than as an
anonymous function), then you need to ensure that the name
LessThanFunc
is visible 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"]
|