sort rtable by row, column, or other 1-D slice
SortBy( rtable1D, options )
SortBy( rtable2D, column_or_row, index, options )
SortBy( rtableND, Indices, options )
A non-empty 1-D rtable of data
A non-empty 2-D rtable of data
A non-empty rtable of data having one or more dimensions
Either column or row, specifies if a column or row will define the sorting
An integer that specifies the specific column or row that will define the sorting
A list specifies the column, row, or 1-D slice that will define the sorting
inplace: Either true or false, specifies if the sorted rtable is to be stored in the original rtable.
output: The type of output. The supported options are:
permutation: Returns the permutation list which is used to perform the sorting.
sorted: Returns the sorted rtable. This is the default.
list of any of the above options: Returns an expression sequence with the corresponding outputs, in the same order.
The SortBy command sorts the given rtable using the specified column, row, or 1-D slice. For example, if A is a Matrix consisting of 10 rows and 5 columns, SortBy⁡A,column,3 will return the Matrix formed by sorting the rows of A in such a way that the elements in column 3 will be non-decreasing.
Any additional arguments to SortBy are passed as additional arguments to the base sort command. The keywords ascending and descending are also accepted.
For an rtable of arbitrary number of dimensions, say n, the Indices list specifies the 1-D slice that defines the sorting. This list must have size n, with elements consisting of a single instance of .. and n−1 integers.
For the 1-D case, SortBy⁡A,.. is equivalent to SortBy⁡A.
For the 2-D case, SortBy⁡A,i,.. is equivalent to SortBy⁡A,row,i and SortBy⁡A,column,j is equivalent to SortBy⁡A,..,j.
When the starting indices for the rtable are all 1, then negative indices (for indexing from the end) can be used in addition to positive ones. More precisely, for a dimension of size n, the allowable indices are the integers 1,..,n (for indexing from the beginning) and −1,..,−n (for indexing from the end).
When one or more dimensions of the rtable has starting index different than 1, then only absolute indices are allowed. More precisely, if a dimension has range a..b, then the allowable indices are the integers a,..,b.
If an rtable has starting indices different than 1 and the sorting permutation is returned, the permutation will the same as if the rtable had its indices shifted to all start at 1.
The rtable must have rectangular storage.
A ≔ Vector'row'⁡9,0,16,4,1
A ≔ Matrix⁡8,18,9,3,16,7,11,1,15,4,13,12
Custom sorting options can be passed:
F ≔ Matrix⁡1,−2,3,−4,5,−6,7,−8,9
G ≔ SortBy⁡F,'row',2
H ≔ SortBy⁡F,'row',2,'key'=abs
The unsorted rtable can be updated with the sorted version:
A ≔ Matrix⁡16,9,5,8,13,19,7,6,17,15
B ≔ SortBy⁡A,'column',−1,'descending','inplace'
Arrays with starting indices different than 1 are accepted:
A ≔ Array⁡0..2,0..4,4,15,8,2,18,5,18,2,3,17,11,15,20,7,1
For this Array, "row 1" refers to what would be "row 2" if the starting indices were shifted to all be 1:
B ≔ SortBy⁡A,'row',1
Consider the following 3-D Array:
The data can be sorted using any 1-D slice, and the sorting permutation can be returned in addition to or instead of the sorted Array. For example:
P,Y ≔ SortBy⁡X,2,..,1,'descending','output'='permutation','sorted'
The sorted Array Y can be reproduced using this permutation P:
Z ≔ X..,P,..
The ArrayTools[SortBy] command was introduced in Maple 2023.
For more information on Maple 2023 changes, see Updates in Maple 2023.
Download Help Document