sort
sort a list of values or a polynomial
Calling Sequence
Parameters
Description
Ordering / Comparison Function
Options
Thread Safety
Objects Supporting Sort
Examples
Compatibility
sort(L)
sort(L, F, options)
sort['inplace'](L, F, options)
L
-
list, Vector, or one-dimensional Array; values to be sorted
F
(optional) symbol or Boolean function of two arguments
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.
`<`
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, F⁡a,b returns false if and only if b must precede a in the sorted output. That is F⁡a,b is a non-strict less than comparison function. In addition, F⁡a,b must be defined for all pairs a,b for a and b in the input structure and F⁡a,b must be transitive, that is, if F⁡a,b=true and F⁡b,c=true then F⁡a,c=true.
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.
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 O⁡n times, whereas a comparison function will be called O⁡n⁢log⁡n 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 a=sort⁡L,output=permutation for a list L, then sort⁡L could be obtained as La. (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 A⁡a.)
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 F⁡a,b returns true if and only if a must precede b 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.
The sort command is thread safe as of Maple 15, provided that a polynomial is not being sorted. Sorting polynomials is not thread safe.
For more information on thread safety, see index/threadsafe.
The following objects support the sort command with a separate implementation. See the help pages linked below for more details.
"DataFrame" objects
"DataSeries" objects
Sorting numbers
sort⁡2,1,3,numeric
1,2,3
sort⁡2,1,3,`>`
3,2,1
undefined is positioned at the end of the sorted list
sort⁡−∞,−1,1,2,1.123,undefined,∞,numeric
−∞,−1,1,1.123,2,∞,undefined
Sorting strings and names
sort⁡c,a,d,lexorder
a,c,d
sort⁡a,A,Z,b
A,Z,a,b
sort⁡a,A,Z,b,StringTools:-Compare
sort⁡a,c,Z,B,StringTools:-CompareCI
a,B,c,Z
Note that using case-insensitive compare will apply stable ordering of elements that resolve to the same string but have different capitalization.
StringTools:-CompareCI⁡abc,ABC
true
StringTools:-CompareCI⁡ABC,abc
Ensure lexorder is specified when sorting names and indexed names, as the default algorithm may not produce what you expect
sort⁡x,x1,x1,2,x3,lexorder
x,x1,x1,2,x3
Sorting by element length
sort⁡a,ba,aaa,aa,length
a,ba,aa,aaa
sort⁡a,ba,aaa,aa,length,descending
aaa,aa,ba,a
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:
sort⁡3,undefined,1,0,2,ecmp,pass=0,undefined
1,2,3,0,undefined
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.
z1≔three,four,two:
z2≔3,4,2:
permutation≔sort⁡z2,output=permutation
permutation≔3,1,2
z1permutation
two,three,four
z2permutation
2,3,4
Or, we can use the output=[sorted,permutation] option to get both the sorted result and the permutation in one call.
z3≔five,eight,two,four:
z4≔Array⁡5,8,2,4:
result,permutation2≔sort⁡z4,output=sorted,permutation
result,permutation2≔2458,3,4,1,2
result
2458
z3permutation2
twofourfiveeight
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.
sort⁡x2,x,x5,numeric,`=`⁡key,x↦degree⁡x
x,x2,x5
sort⁡x2,x,x5,numeric,key=degree,descending
x5,x2,x
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.
l≔LibraryTools:-ShowContents⁡:
sl≔sort⁡l,lexorder1:
sl1..10
#msup(mi("a"),mo("+")).m,2025,3,24,5,18,4,111489679,44,#msup(mi("a"),mo("−")).m,2025,3,24,5,18,4,175576165,47,%?.m,2025,3,24,5,10,1,86894368,35,%T.m,2025,3,24,5,10,1,111136666,35,%assuming.m,2025,3,24,5,15,11,143482223,448,∞.m,2025,3,24,5,19,7,92059411,50,&where.m,2025,3,24,5,15,11,66035846,737,-.m,2025,3,24,5,15,21,164935756,303,....m,2025,3,24,5,10,1,186670274,36,..m,2025,3,24,5,10,12,85988192,3410
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.
sort⁡1,1,2,1,3,1,1,2,2,2,3,2,1,3,2,3,3,3,x,y↦x1≤y1
1,1,1,2,1,3,2,1,2,2,2,3,3,1,3,2,3,3
Strict less than
Specifying the strict option allows the use of a strict less than comparison function while still producing stable sorting.
sort⁡1,1,2,1,3,1,1,2,2,2,3,2,1,3,2,3,3,3,`=`⁡strict,x,y↦x1<y1
This example could also be sorted using the key option.
sort⁡1,1,2,1,3,1,1,2,2,2,3,2,1,3,2,3,3,3,numeric,`=`⁡key,x↦x1
Sorting in-place
This example sorts a Vector in-place. Notice that myVec is changed after calling sort
myVec≔2,1,4
myVec≔214
sortinplace⁡myVec
124
myVec
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
myList≔2,1,4
sortinplace⁡myList
1,2,4
myList
2,1,4
Set ordering
Set ordering relies on attributes such as the kind of datatype and its length among other things:
L≔a,aaaaaaaa,b,bbbbbbbb,a,aaaaaaaa,b,bbbbbbbb
seq⁡sort⁡L,setorderi,i=1..8
a,b,aaaaaaaa,bbbbbbbb,a,b,aaaaaaaa,bbbbbbbb,b,a,bbbbbbbb,aaaaaaaa,b,a,bbbbbbbb,aaaaaaaa,aaaaaaaa,bbbbbbbb,a,b,aaaaaaaa,bbbbbbbb,a,b,bbbbbbbb,aaaaaaaa,b,a,bbbbbbbb,aaaaaaaa,b,a,aaaaaaaa,bbbbbbbb,a,b,aaaaaaaa,bbbbbbbb,a,b,bbbbbbbb,aaaaaaaa,b,a,bbbbbbbb,aaaaaaaa,b,a,a,b,aaaaaaaa,bbbbbbbb,a,b,aaaaaaaa,bbbbbbbb,b,a,bbbbbbbb,aaaaaaaa,b,a,bbbbbbbb,aaaaaaaa
The output option was introduced in Maple 17.
For more information on Maple 17 changes, see Updates in Maple 17.
The key, nonstrict and strict options were introduced in Maple 18.
For more information on Maple 18 changes, see Updates in Maple 18.
The inplace option was introduced in Maple 2016.
For more information on Maple 2016 changes, see Updates in Maple 2016.
The pass option was introduced in Maple 2025.
The descending, ascending, nonstrict and strict options were updated in Maple 2025.
For more information on Maple 2025 changes, see Updates in Maple 2025.
See Also
ArrayTools:-SortBy
DataFrame/sort
list
rtable
sort/polynomial
Statistics:-Sort
StringTools:-Sort
Download Help Document