Function: sort (<L>, <P>)
Function: 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
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
ordergreatp sorts a list in descending order. The predicate
ordermagnitudep sorts Maxima numbers, constant symbols with a numerical value, or expressions which can be evaluated to a constant by magnitude. All other elements of the list <L> are sorted by
orderlessp. The predicate
"<" allows the ordering by magnitude too, but does not order completely if elements in the list <L> are not comparable under
(%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]]
Order Maxima numbers, constants, and constants expressions by magnitude, and all other elements in ascending sorting order:
(%i8) sort([%i,1+%i,2*x,minf,inf,%e,sin(1),0,1,2,3,1.0,1.0b0], ordermagnitudep); (%o8) [minf, 0, sin(1), 1, 1.0, 1.0b0, 2, %e, 3, inf, %i, %i + 1, 2 x]
There are also some inexact matches for
?? sort to see them.
(%o1) true (%i2)