sort -- sort a list
Introductionsort(list) returns a sorted copy of the
list.
Call(s)sort(list <, f>)
Parameterslist |
- | a list of arbitrary MuPAD objects |
f |
- | a procedure defining the ordering |
Returnsa list.
list
Related
Functions
Detailssort sorts the list in ``ascending order''.f is specified by the user, lists are
sorted as follows:
Type::Real is sorted numerically.sort(list) is
equivalent to sort(list, sysorder).
The internal order is a well-ordering. It is not session dependent, but may differ between different MuPAD versions. No special features of the internal sorting mechanism should be assumed by the user. Such sorting may be useful to produce a unique representation of lists.
When strings are compared, capital letters are
sorted in front of small letters. E.g., "Z" is smaller
than "abc".
Sets and tables do not have a unique internal order
(sysorder).
Consequently, sorting does not lead to a unique ordering, if elements
of the list are sets or tables, or contain sets or tables as
(sub)operands. Cf. example 2.
f may be specified to define the sorting
criteria. It is used to compare the ordering of pairs of list elements
and is called in the form f(x, y) with elements
x, y from the list. It must return a Boolean
expression that can be evaluated to either TRUE or FALSE. TRUE indicates that x is
to be sorted left of y. Consequently, the elements of the
ordered list L := sort(list, f) satisfy bool(f(L[i], L[j])) = TRUE
for i < j.
If the ordering provided by f is not a well-ordering,
sorting is not 'stable' and elements with the same order may be
swapped.
sort is a function of the system kernel.
Example
1Real numbers of syntactical type Type::Real are sorted numerically:
>> sort([4, -1, 2/3, 0.5])
[-1, 0.5, 2/3, 4]
Strings are sorted lexicographically:
>> sort(["chip", "alpha", "Zip"])
["Zip", "alpha", "chip"]
Other types of objects are sorted according to their internal ordering. This also holds for lists with elements of different types:
>> sort([4, -1, 2/3, 0.5, "alpha"])
["alpha", -1, 4, 0.5, 2/3]
>> sort([4, -1, 2/3, 0.5, I])
[-1, 4, 0.5, 2/3, I]
Example
2There is no unique internal order for sets and tables:
>> sort([{1}, {2}]) <> sort([{2}, {1}])
[{1}, {2}] <> [{2}, {1}]
>> sort([table("a" = 42), table("a" = 43)]) <>
sort([table("a" = 43), table("a" = 42)])
-- table( table( --
| "a" = 42 , "a" = 43 | <>
-- ) ) --
-- table( table( --
| "a" = 43 , "a" = 42 |
-- ) ) --
Example
3The following list is sorted according to a user-defined criteron:
>> sort([-2, 1, -3, 4], (x, y) -> abs(x) < abs(y))
[1, -2, -3, 4]
Background