|
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, 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 .
|
|
|
•
|
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 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.
|
|
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 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.
|
•
|
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 for a list , then could be obtained as . (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 .)
|
|
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
|
•
|
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.
|
|
|
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
Sorting strings and names
Sorting by element length
Sorting by internal memory address
Obtaining the sort permutation vector
Sorting polynomials
The powers in an algebraic expression do not have to be positive integers. They can be negative integers or fractions.
Sorting by the first element of a list
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.
Specifying the strict option allows the use of a strict less than comparison function while still producing stable sorting.
This example could also be sorted using the key option.
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
Set ordering relies on attributes such as the kind of datatype and its length among other things:
|
|
Compatibility
|
|
•
|
The output option was introduced in Maple 17.
|
•
|
The comptype option was updated in Maple 18.
|
•
|
The inplace option was introduced in Maple 2016.
|
|
|
|