Maxima Function
sort (L, P)
sort(L)
Sorts a list L according to a predicate P
of two arguments,
such that P (L[k], L[k + 1])
is true
for any two successive elements.
The predicate may be specified as the name of a function or binary infix operator,
or as a lambda
expression.
If specified as the name of an operator,
the name is enclosed in "double quotes".
The sorted list is returned as a new object;
the argument L is not modified.
To construct the return value,
sort
makes a shallow copy of the elements of L.
If the predicate P is not a total order on the elements of L,
then sort
might run to completion without error,
but the result is undefined.
sort
complains if the predicate evaluates to something other
than true
or false
.
sort (L)
is equivalent to sort (L, orderlessp)
.
That is, the default sorting order is ascending,
as determined by orderlessp
.
All Maxima atoms and expressions are comparable under orderlessp
,
although there are isolated examples of expressions for which orderlessp
is not transitive;
this is a bug.
Examples:
(%i1) sort ([11, -17, 29b0, 7.55, 3, -5/2, b + a, 9 * c, 19 - 3 * x]); 5 (%o1) [- 17, - -, 3, 7.55, 11, 2.9b1, b + a, 9 c, 19 - 3 x] 2 (%i2) sort ([11, -17, 29b0, 7.55, 3, -5/2, b + a, 9*c, 19 - 3*x], ordergreatp); 5 (%o2) [19 - 3 x, 9 c, b + a, 2.9b1, 11, 7.55, 3, - -, - 17] 2 (%i3) sort ([%pi, 3, 4, %e, %gamma]); (%o3) [3, 4, %e, %gamma, %pi] (%i4) sort ([%pi, 3, 4, %e, %gamma], "<"); (%o4) [%gamma, %e, 3, %pi, 4] (%i5) my_list: [[aa,hh,uu], [ee,cc], [zz,xx,mm,cc], [%pi,%e]]; (%o5) [[aa, hh, uu], [ee, cc], [zz, xx, mm, cc], [%pi, %e]] (%i6) sort (my_list); (%o6) [[%pi, %e], [aa, hh, uu], [ee, cc], [zz, xx, mm, cc]] (%i7) sort (my_list, lambda ([a, b], orderlessp (reverse (a), reverse (b)))); (%o7) [[%pi, %e], [ee, cc], [zz, xx, mm, cc], [aa, hh, uu]]