sort
sort a list of values or a polynomial
Calling Sequence
Parameters
Description
Thread Safety
Objects supporting sort
Examples
Compatibility
sort(L)
sort(L, output=out)
sort(L, `>`, output=out)
sort(L, `<`, output=out)
sort(L, address, output=out)
sort(L, length, output=out)
sort(L, lexorder, output=out)
sort(L, F)
sort(L, comptype = F)
sort(L, F, output=out)
sort(L, comptype = F, output=out)
sort(A)
sort(A, V, opt)
sort(A, order=o, opt)
sort[inplace](A)
L
-
list, Vector, or one-dimensional Array; values to be sorted
out
(optional) sorted or permutation or [sorted, permutation]
F
(optional) symbol or Boolean function of two arguments (sort ordering), or key function (when comptype is key)
comptype
(optional) nonstrict (the default), strict, or key; comparison type
A
algebraic; expression to be sorted
V
(optional) variables
o
(optional) monomial order
opt
(optional) either ascending or descending
By default, the sort command sorts the elements of a list, Vector, or one-dimensional Array, L, into ascending order and sorts the terms of polynomials in an algebraic expression A into descending order.
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.
If F is given, it specifies the ordering for sorting elements.
`<`, numeric
If F is the symbol `<` or numeric, and L contains only elements of type({numeric, real_infinity}), then L is sorted in ascending numerical order.
`<`
If F is the symbol `<`, and L contains only strings then L is sorted in lexicographic order.
If F is the symbol `<` and 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 into descending numerical order.
If F is the symbol `>`, and L contains only strings then L is sorted in lexicographic order.
If F is the symbol `>` and 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).
length
If F is the symbol length, then the elements are sorted by length where length is as determined by the length function.
lexorder
If F is the symbol lexorder or string, then lists of strings or symbols are sorted into lexicographic order.
'lexorder'[n]
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.
'setorder', 'setorder'[n]
If F is the symbol setorder then the elemenst 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
Otherwise F 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.
By specifying comptype=F, different styles of comparison functions can be given to sort. These are:
nonstrict=F - The comparison function F is a non-strict less than function. This is the default, as described above.
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.
key=F - The function F 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.
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 will be 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.
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 1-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, one can supply the argument output = [sorted, permutation]. This will return a sequence of two elements, the first being the sorted argument, the second the permutation.
In Maple, polynomials are not automatically stored in sorted order. They are stored in the order they were first created and printed in the order they are stored. The sort command can be used to sort polynomials.
Note: Sorting polynomials is a destructive operation: the input polynomial is sorted "in-place".
If V is given, it specifies the variable ordering to be used when sorting polynomials. It can be a name, a function, or a list or set of names (for the multivariate case).
All polynomials in the expression A are sorted into decreasing order in V. If V is a list or set then polynomials in V are sorted in total degree with ties broken by lexicographic order (this is called graded lexicographic order). If neither V nor o is specified then the indets appearing in A and graded lexicographic order are used.
Other monomial orders can be specified by using the order=o calling sequence. The supported orders are:
plex(x1, ..., xn) - lexicographic order
grlex(x1, ..., xn) - graded lexicographic order
tdeg(x1, ..., xn) - graded reverse lexicographic order
for indeterminates x1, ..., xn. For a description of these orders, see Monomial orders for multivariate polynomials.
When sorting polynomials the optional argument ascending or descending may be specified to put the terms into ascending or descending order, respectively. The default is descending order, which puts higher-degree terms before lower-degree terms.
The sorting algorithm used for sorting lists is a recursive implementation of Merge sort with early detection of sorted sequences. The sorting algorithm used for sorting polynomials is an in-place Shell sort. Lexicographic ordering of strings and symbols assumes the collating sequence of the US-ASCII character set.
When called using the indexed name, sort['inplace'], the command will attempt 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.
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
1,2,3
sort⁡2,1,3,numeric
sort⁡2,1,3,`>`
3,2,1
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,A,b,a↦StringTools:−Compare⁡a,b
c,a,Z,A
sort⁡a,c,Z,A,`>`
Sorting by element length
sort⁡a,ba,aaa,aa,length
a,ba,aa,aaa
Sorting by internal memory address
sort⁡a,ax,bb,bf,address
ax,bb,bf,a
Obtaining the sort permutation vector
z1≔three,four,two
z2≔3,4,2
permutation≔sort⁡z2,output=permutation
permutation≔3,1,2
z1permutation
two,three,four
z3≔five,eight,two,four
z3≔fiveeighttwofour
z4≔Array⁡5,8,2,4
z4≔5824
result,permutation2≔sort⁡z4,output=sorted,permutation
result,permutation2≔2458,3,4,1,2
result
2458
z3permutation2
twofourfiveeight
Sorting polynomials
sort⁡1+x+x2
x2+x+1
f≔4⁢x3+5⁢x2⁢z2+2⁢x⁢y2⁢z+1
f≔5⁢x2⁢z2+2⁢x⁢y2⁢z+4⁢x3+1
sort⁡f,x,y,z
5⁢x2⁢z2+2⁢x⁢y2⁢z+4⁢x3+1
sort⁡f,x,y,z,ascending
1+4⁢x3+2⁢x⁢y2⁢z+5⁢x2⁢z2
sort⁡f,order=grlex⁡x,y,z
sort⁡f,order=tdeg⁡x,y,z
2⁢x⁢y2⁢z+5⁢x2⁢z2+4⁢x3+1
sort⁡f,order=plex⁡x,y,z
4⁢x3+5⁢x2⁢z2+2⁢x⁢y2⁢z+1
sort⁡f,order=plex⁡x,y,z,descending
sort⁡f,order=plex⁡x,y,z,ascending
1+2⁢x⁢y2⁢z+5⁢x2⁢z2+4⁢x3
sort⁡y+xy−x,x
x+y−x+y
sort⁡a⁢x+b⁢y+c⁢x2,x
c⁢x2+a⁢x+b⁢y
sort⁡a⁢x+b⁢y+c⁢x2,x,ascending
b⁢y+a⁢x+c⁢x2
The powers in an algebraic expression do not have to be positive integers. They can be negative integers or fractions.
f≔ax2+bx6+cx4+dx32
sort⁡f,x
dx32+ax2+cx4+bx6
Sorting by the first element of a list
l≔LibraryTools:-ShowContents⁡:
sl≔sort⁡l,lexorder1:
sl1..10
#msup(mi("a"),mo("+")).m,2024,2,29,22,21,37,98469712,44,#msup(mi("a"),mo("−")).m,2024,2,29,22,21,37,129338421,47,%?.m,2024,2,29,21,51,17,45806319,35,%T.m,2024,2,29,21,51,17,98297345,35,%assuming.m,2024,2,29,22,12,3,102572360,452,∞.m,2024,2,29,22,25,1,52673668,50,&where.m,2024,2,29,22,12,2,34841415,737,-.m,2024,2,29,22,13,55,107656668,303,....m,2024,2,29,21,51,18,164185474,36,..m,2024,2,29,21,51,35,44130671,3410
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
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,`=`⁡key,x↦x1
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 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 comptype option was updated in Maple 18.
The inplace option was introduced in Maple 2016.
For more information on Maple 2016 changes, see Updates in Maple 2016.
See Also
degree
Groebner/MonomialOrders
indets
list
polynom
Download Help Document