|
Calling Sequence
|
|
sort(L)
sort(L, F, options)
sort['inplace'](L, F, options)
|
|
Parameters
|
|
L
|
-
|
list, Vector, or one-dimensional Array; values to be sorted
|
F
|
-
|
(optional) symbol or Boolean function of two arguments
|
|
|
|
|
Description
|
|
•
|
By default, the sort command sorts the elements of a list, Vector, or one-dimensional Array, L, into ascending order. See the sort/polynomial help page for details on sorting the terms of polynomials in an algebraic expression.
|
•
|
sort is stable when applied to L. This means that equal elements are ordered by their position in L. In other words, the relative order of equal elements is maintained.
|
•
|
The sorting algorithm used for sorting lists is a recursive implementation of Merge sort with early detection of sorted sequences.
|
•
|
If F is given, it specifies the ordering for sorting elements.
|
|
|
Ordering / Comparison Function
|
|
|
`<`
|
|
|
If F is the symbol `<` and L contains only elements of type({numeric, real_infinity}), then L is sorted in ascending numerical order.
|
|
If L contains only strings then L is sorted in lexicographic order.
|
|
If L contains any elements which are objects implementing a `<` method, then L is sorted using that method as a comparison function.
|
|
|
`>`
|
|
|
If F is the symbol `>` and L contains only elements of type({numeric, real_infinity}), then L is sorted in descending numerical order.
|
|
If L contains only strings then L is sorted in reverse lexicographic order.
|
|
If L contains any elements which are objects implementing a `<` method, then L is sorted using the inverse of that method as a comparison function.
|
|
|
address
|
|
|
If F is the symbol address, then the elements are sorted by address (a non-deterministic run-time specific property of the underlying data structure).
|
|
|
general
|
|
|
If F is the symbol general, then the elements are sorted in the following way:
|
–
|
numeric values are sorted in ascending numerical order
|
–
|
symbols and strings are sorted in lexorder (ASCII order)
|
–
|
indexed names and unevaluated functions are sorted first by the name part, then, if both objects being compared have an index, then the index or argument sequence
|
–
|
lists, sets, and rtables are compared element-wise as long as there are still elements in the container. The shorter container is considered less-than if it entirely matches the first n-elements of the other container. Multi-dimension rtables are scanned in memory order, which is column-major by default.
|
–
|
specialized types are sorted by their id-classification, or objectid
|
|
|
length
|
|
|
If F is the symbol length, then the elements are sorted by length where length is as determined by the length function.
|
|
|
lexorder, 'lexorder'[n], string
|
|
|
If F is the symbol lexorder or string, then lists of strings or symbols are sorted into lexicographic order. If the list also contains indexed names, then the general comparison method is used.
|
|
If L is a list of lists and F is the indexed symbol lexorder[n], where n is a positive integer, then L is sorted into lexicographic order based on the nth element of each sublist of L.
|
|
Lexicographic ordering of strings and symbols assumes the collating sequence of the US-ASCII character set.
|
|
|
numeric
|
|
|
If F is the symbol numeric and L contains only elements of type(extended_numeric), then L is sorted in ascending numerical order with undefined at the end.
|
|
|
'setorder', 'setorder'[n]
|
|
|
If F is the symbol setorder then the elements are sorted the same way set elements are sorted. The "--setsort=n" command-line argument allows you to change the default ordering of sets. These same orderings can be used by choosing setorder[n] for n, an integer in the range 1..8.
|
|
|
custom boolean procedure
|
|
|
When F does not match one of the symbols listed above, it must be a Boolean-valued function of two arguments. Specifically, returns if and only if must precede in the sorted output. That is is a non-strict less than comparison function. In addition, must be defined for all pairs for and in the input structure and must be transitive, that is, if and then .
|
|
|
no ordering function
|
|
|
If no ordering function F is specified, a list, Vector, or Array is sorted as follows:
|
–
|
If all elements are of type {numeric, real_infinity}, they are sorted into increasing numerical order.
|
–
|
If all elements are strings or symbols, they are sorted in lexicographical order.
|
–
|
If any elements are objects implementing a `<` method, that method is used for any comparison where either element is such an object, and any other elements are compared using the same rules used to sort sets as described below.
|
–
|
Otherwise, all elements of L are sorted by the same rules used to sort sets. This mixed-element sort is optimized to balance speed and "niceness" of the result. Data types of the same structure are grouped together; within that internal object length is used as the next criteria; further down, individual properties such as numeric order are used when comparing elements during the sort process.
|
|
|
|
Options
|
|
|
ascending, descending
|
|
|
The option descending=true can be used to reverse the order of the comparison.
|
|
The option ascending=true is the default. Reversing the order can also be achieved by specifying ascending=false.
|
|
|
inplace
|
|
|
When using the option inplace=true or when called using the indexed name sort['inplace'], the command attempts to sort the given Array or Vector in-place. If the first argument is not a mutable data structure, such as a list, then the sorting will be done in a copy. Similarly, if the source rtable has an indexing function that cannot be preserved during sort, such as scalar[], then the result will be a copy.
|
|
|
key=KF
|
|
|
The function KF(a) -> a' maps each element in L to a key value. L is sorted by sorting the corresponding keys. Using a key function is preferable to a comparison function because the key function is called times, whereas a comparison function will be called times. This is generally faster. In addition, sorting the keys may be done in parallel, whereas this may not be possible with a comparison function.
|
|
The key function, KF, can be used in addition to a custom comparison function, F.
|
|
|
nonstrict=true, or nonstrict=F
|
|
|
The comparison function F is a non-strict less than function. This is the default, as detailed in the "Description" above.
|
|
The syntax nonstrict=F can be used in place of F as the second argument, or the syntax sort(L,F,nonstrict=true) can be used to achieve the same result.
|
|
|
output=permutation, output=[sorted,permutation]
|
|
|
If an argument output=permutation is supplied, then sort does not return the sorted argument, but the permutation that would be applied to the argument in order to sort it. The permutation is given as a list of integers: the ith entry of the permutation is the integer j such that the jth entry of L would occur at the ith position in the sorted argument. This means that if for a list , then could be obtained as . (For one-dimensional Arrays A, there may be an offset between the jth entry and the entry obtained as A[j], but the correctly sorted result can be obtained using programmer indexing as .)
|
|
If an argument output = sorted is supplied, sort returns the sorted argument. This is the default behavior. In order to obtain both the sorted argument and the permutation, specify the option output = [sorted, permutation]. This returns a sequence of two elements, the first being the sorted argument, the second the permutation.
|
|
|
pass=(F_args)
|
|
|
The pass option can be used to pass additional arguments to the custom comparison function. F is a function that accepts two arguments, (a,b) and returns true or false. A simple ascending sort may look like this: sort( [3,1,-2], (a,b)->a <= b ). By using the pass option, F can be passed additional arguments. sort( [3,1,-2], (a,b,f)->f(a) <= f(b), 'pass'=abs ).
|
|
|
strict=true, or strict=F
|
|
|
The comparison function F is a strict less than function. That is returns if and only if must precede in the sorted output. F must still be defined for all pairs of inputs and be transitive, as described above. This argument is necessary if you want to specify a less than or equal to comparison function and want stable sorting. Specifying a strict less than function without using the strict option will result in an non-stable, sorted output.
|
|
The syntax strict=F can be used to replace F as the second argument, or the syntax sort(L,F,strict=true) can be used to achieve the same result.
|
|
|
|
Thread Safety
|
|
•
|
The sort command is thread safe as of Maple 15, provided that a polynomial is not being sorted. Sorting polynomials is not thread safe.
|
|
|
Objects Supporting Sort
|
|
•
|
The following objects support the sort command with a separate implementation. See the help pages linked below for more details.
|
|
|
Examples
|
|
|
Sorting numbers
|
|
>
|
|
>
|
|
|
undefined is positioned at the end of the sorted list
|
>
|
|
| (3) |
|
|
Sorting strings and names
|
|
>
|
|
>
|
|
| (5) |
>
|
|
| (6) |
>
|
|
| (7) |
|
Note that using case-insensitive compare will apply stable ordering of elements that resolve to the same string but have different capitalization.
|
>
|
|
>
|
|
|
Ensure lexorder is specified when sorting names and indexed names, as the default algorithm may not produce what you expect
|
>
|
|
| (10) |
|
|
Sorting by element length
|
|
>
|
|
| (11) |
>
|
|
| (12) |
|
|
Passing arguments to the comparison function
|
|
|
This example uses the pass option to allow a third argument to the comparison procedure. Here, the {0,undefined} set will be passed as the third argument in each call to ecmp.
|
>
|
ecmp := proc(a,b,exclude)
if a in exclude then
false;
elif b in exclude then
true;
else
a <= b;
end if;
end proc:
|
>
|
|
| (13) |
|
|
Obtaining the sort permutation vector
|
|
|
Here we have two lists and want to sort them both according to the same ordering. This is done by finding a permutation vector, then applying it to both lists.
|
>
|
|
>
|
|
>
|
|
| (14) |
| (15) |
|
Or, we can use the output=[sorted,permutation] option to get both the sorted result and the permutation in one call.
|
>
|
|
>
|
|
>
|
|
| (17) |
|
|
Sorting expressions using a key function.
|
|
|
Here the key function is applied first, computing the degree of each element, then the list is sorted by those degrees.
|
>
|
|
>
|
|
|
|
Sorting by the first element of a list
|
|
|
Here we have a list where each entry is a sublist of the form [name,date,offset,size]. We want to sort by the name element at position 1 in the sublist.
|
>
|
|
>
|
|
| (22) |
|
|
Stable sorting
|
|
|
An example of stable sorting. Sort based on the first element in the sublist. Notice that sublists whose first elements are equal are ordered based on their position in the input list.
|
>
|
|
| (23) |
|
|
Strict less than
|
|
|
Specifying the strict option allows the use of a strict less than comparison function while still producing stable sorting.
|
>
|
|
| (24) |
This example could also be sorted using the key option.
>
|
|
| (25) |
|
|
Sorting in-place
|
|
This example sorts a Vector in-place. Notice that myVec is changed after calling sort
>
|
|
This example ignores the inplace option because it is given a non-mutable list to sort. Notice that myList is *not* changed after calling sort
>
|
|
| (29) |
|
|
Set ordering
|
|
Set ordering relies on attributes such as the kind of datatype and its length among other things:
>
|
|
| (32) |
>
|
|
| (33) |
|
|
|
Compatibility
|
|
•
|
The output option was introduced in Maple 17.
|
•
|
The key, nonstrict and strict options were introduced in Maple 18.
|
•
|
The inplace option was introduced in Maple 2016.
|
•
|
The pass option was introduced in Maple 2025.
|
•
|
The descending, ascending, nonstrict and strict options were updated in Maple 2025.
|
|
|
|