sort - Maple Help
For the best experience, we recommend viewing online help using Google Chrome or Microsoft Edge.

Online Help

All Products    Maple    MapleSim


sort

sort a list of values or a polynomial

 

Calling Sequence

Parameters

Description

Thread Safety

Objects supporting sort

Examples

Compatibility

Calling Sequence

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)

Parameters

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

Description

• 

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, Fa&comma;b returns false if and only if b must precede a in the sorted output.  That is Fa&comma;b is a non-strict less than comparison function.  In addition, Fa&comma;b must be defined for all pairs a,b for a and b in the input structure and Fa&comma;b must be transitive, that is, if Fa&comma;b=true and Fb&comma;c=true then Fa&comma;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 Fa&comma;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 On times, whereas a comparison function will be called Onlogn 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=sortL&comma;output=permutation for a list L, then sortL 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 Aa.)

  

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.

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.

• 

For more information on thread safety, see index/threadsafe.

Objects supporting sort

• 

The following objects support the sort command with a separate implementation. See the help pages linked below for more details.

"DataFrame" objects

"DataSeries" objects

 

 

Examples

Sorting numbers

sort2&comma;1&comma;3

1&comma;2&comma;3

(1)

sort2&comma;1&comma;3&comma;numeric

1&comma;2&comma;3

(2)

sort2&comma;1&comma;3&comma;`>`

3&comma;2&comma;1

(3)

Sorting strings and names

sortc&comma;a&comma;d&comma;lexorder

a&comma;c&comma;d

(4)

sorta&comma;A&comma;Z&comma;b

A&comma;Z&comma;a&comma;b

(5)

sorta&comma;A&comma;Z&comma;b&comma;StringTools:-Compare

A&comma;Z&comma;a&comma;b

(6)

sorta&comma;c&comma;Z&comma;A&comma;b&comma;aStringTools:−Comparea&comma;b

c&comma;a&comma;Z&comma;A

(7)

sorta&comma;c&comma;Z&comma;A&comma;`>`

c&comma;a&comma;Z&comma;A

(8)

Sorting by element length

sorta&comma;ba&comma;aaa&comma;aa&comma;length

a&comma;ba&comma;aa&comma;aaa

(9)

Sorting by internal memory address

sorta&comma;ax&comma;bb&comma;bf&comma;address

ax&comma;bb&comma;bf&comma;a

(10)

Obtaining the sort permutation vector

z1three&comma;four&comma;two

z1three&comma;four&comma;two

(11)

z23&comma;4&comma;2

z23&comma;4&comma;2

(12)

permutationsortz2&comma;output=permutation

permutation3&comma;1&comma;2

(13)

z1permutation

two&comma;three&comma;four

(14)

z3five&comma;eight&comma;two&comma;four

z3fiveeighttwofour

(15)

z4Array5&comma;8&comma;2&comma;4

z45824

(16)

result,permutation2sortz4&comma;output=sorted&comma;permutation

result,permutation22458,3&comma;4&comma;1&comma;2

(17)

result

2458

(18)

z3permutation2

twofourfiveeight

(19)

Sorting polynomials

sort1+x+x2

x2+x+1

(20)

f4x3+5x2z2+2xy2z+1

f5x2z2+2xy2z+4x3+1

(21)

sortf&comma;x&comma;y&comma;z

5x2z2+2xy2z+4x3+1

(22)

sortf&comma;x&comma;y&comma;z&comma;ascending

1+4x3+2xy2z+5x2z2

(23)

sortf&comma;order=grlexx&comma;y&comma;z

5x2z2+2xy2z+4x3+1

(24)

sortf&comma;order=tdegx&comma;y&comma;z

2xy2z+5x2z2+4x3+1

(25)

sortf&comma;order=plexx&comma;y&comma;z

4x3+5x2z2+2xy2z+1

(26)

sortf&comma;order=plexx&comma;y&comma;z&comma;descending

4x3+5x2z2+2xy2z+1

(27)

sortf&comma;order=plexx&comma;y&comma;z&comma;ascending

1+2xy2z+5x2z2+4x3

(28)

sorty+xyx&comma;x

x+yx+y

(29)

sortax+by+cx2&comma;x

cx2+ax+by

(30)

sortax+by+cx2&comma;x&comma;ascending

by+ax+cx2

(31)

The powers in an algebraic expression do not have to be positive integers. They can be negative integers or fractions.

fax2+bx6+cx4+dx32

fax2+bx6+cx4+dx32

(32)

sortf&comma;x

dx32+ax2+cx4+bx6

(33)

Sorting by the first element of a list

lLibraryTools:-ShowContents&colon;

slsortl&comma;lexorder1&colon;

sl1..10

#msup(mi("a"),mo("+")).m&comma;2024&comma;2&comma;29&comma;22&comma;21&comma;37&comma;98469712&comma;44&comma;#msup(mi("a"),mo("−")).m&comma;2024&comma;2&comma;29&comma;22&comma;21&comma;37&comma;129338421&comma;47&comma;%?.m&comma;2024&comma;2&comma;29&comma;21&comma;51&comma;17&comma;45806319&comma;35&comma;%T.m&comma;2024&comma;2&comma;29&comma;21&comma;51&comma;17&comma;98297345&comma;35&comma;%assuming.m&comma;2024&comma;2&comma;29&comma;22&comma;12&comma;3&comma;102572360&comma;452&comma;∞.m&comma;2024&comma;2&comma;29&comma;22&comma;25&comma;1&comma;52673668&comma;50&comma;&where.m&comma;2024&comma;2&comma;29&comma;22&comma;12&comma;2&comma;34841415&comma;737&comma;-.m&comma;2024&comma;2&comma;29&comma;22&comma;13&comma;55&comma;107656668&comma;303&comma;....m&comma;2024&comma;2&comma;29&comma;21&comma;51&comma;18&comma;164185474&comma;36&comma;..m&comma;2024&comma;2&comma;29&comma;21&comma;51&comma;35&comma;44130671&comma;3410

(34)

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.

sort1&comma;1&comma;2&comma;1&comma;3&comma;1&comma;1&comma;2&comma;2&comma;2&comma;3&comma;2&comma;1&comma;3&comma;2&comma;3&comma;3&comma;3&comma;x&comma;yx1y1

1&comma;1&comma;1&comma;2&comma;1&comma;3&comma;2&comma;1&comma;2&comma;2&comma;2&comma;3&comma;3&comma;1&comma;3&comma;2&comma;3&comma;3

(35)

Specifying the strict option allows the use of a strict less than comparison function while still producing stable sorting.

sort1&comma;1&comma;2&comma;1&comma;3&comma;1&comma;1&comma;2&comma;2&comma;2&comma;3&comma;2&comma;1&comma;3&comma;2&comma;3&comma;3&comma;3&comma;`=`strict&comma;x&comma;yx1<y1

1&comma;1&comma;1&comma;2&comma;1&comma;3&comma;2&comma;1&comma;2&comma;2&comma;2&comma;3&comma;3&comma;1&comma;3&comma;2&comma;3&comma;3

(36)

This example could also be sorted using the key option.

sort1&comma;1&comma;2&comma;1&comma;3&comma;1&comma;1&comma;2&comma;2&comma;2&comma;3&comma;2&comma;1&comma;3&comma;2&comma;3&comma;3&comma;3&comma;`=`key&comma;xx1

1&comma;1&comma;1&comma;2&comma;1&comma;3&comma;2&comma;1&comma;2&comma;2&comma;2&comma;3&comma;3&comma;1&comma;3&comma;2&comma;3&comma;3

(37)

This example sorts a Vector in-place.  Notice that myVec is changed after calling sort

myVec2&comma;1&comma;4

myVec214

(38)

sortinplacemyVec

124

(39)

myVec

124

(40)

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

myList2&comma;1&comma;4

myList2&comma;1&comma;4

(41)

sortinplacemyList

1&comma;2&comma;4

(42)

myList

2&comma;1&comma;4

(43)

Set ordering relies on attributes such as the kind of datatype and its length among other things:

La&comma;aaaaaaaa&comma;b&comma;bbbbbbbb&comma;a&comma;aaaaaaaa&comma;b&comma;bbbbbbbb

La&comma;aaaaaaaa&comma;b&comma;bbbbbbbb&comma;a&comma;aaaaaaaa&comma;b&comma;bbbbbbbb

(44)

seqsortL&comma;setorderi&comma;i=1..8

a&comma;b&comma;aaaaaaaa&comma;bbbbbbbb&comma;a&comma;b&comma;aaaaaaaa&comma;bbbbbbbb,b&comma;a&comma;bbbbbbbb&comma;aaaaaaaa&comma;b&comma;a&comma;bbbbbbbb&comma;aaaaaaaa,aaaaaaaa&comma;bbbbbbbb&comma;a&comma;b&comma;aaaaaaaa&comma;bbbbbbbb&comma;a&comma;b,bbbbbbbb&comma;aaaaaaaa&comma;b&comma;a&comma;bbbbbbbb&comma;aaaaaaaa&comma;b&comma;a,aaaaaaaa&comma;bbbbbbbb&comma;a&comma;b&comma;aaaaaaaa&comma;bbbbbbbb&comma;a&comma;b,bbbbbbbb&comma;aaaaaaaa&comma;b&comma;a&comma;bbbbbbbb&comma;aaaaaaaa&comma;b&comma;a,a&comma;b&comma;aaaaaaaa&comma;bbbbbbbb&comma;a&comma;b&comma;aaaaaaaa&comma;bbbbbbbb,b&comma;a&comma;bbbbbbbb&comma;aaaaaaaa&comma;b&comma;a&comma;bbbbbbbb&comma;aaaaaaaa

(45)

Compatibility

• 

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

length

lexorder

list

polynom