ArrayTools
SortBy
sort rtable by row, column, or other 1-D slice
Calling Sequence
Parameters
Options
Description
Examples
Compatibility
SortBy( rtable1D, options )
SortBy( rtable2D, column_or_row, index, options )
SortBy( rtableND, Indices, options )
rtable1D
-
nonempty 1-D rtable of data
rtable2D
nonempty 2-D rtable of data
rtableND
nonempty rtable of data having one or more dimensions
column_or_row
either column or row, specifies if a column or row defines the sorting
index
An integer that specifies the specific column or row that will define the sorting
Indices
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.
with⁡ArrayTools:
Example 1
A≔Vectorrow⁡9,0,16,4,1
A≔901641
SortBy⁡A
014916
Example 2
A≔Matrix⁡8,18,9,3,16,7,11,1,15,4,13,12
A≔818931671111541312
SortBy⁡A,column,2
111154131231678189
SortBy⁡A,row,−1
891837161115141213
Example 3
Custom sorting options can be passed:
F≔Matrix⁡1,−2,3,−4,5,−6,7,−8,9
F≔1−23−45−67−89
G≔SortBy⁡F,row,2
G≔31−2−6−4597−8
H≔SortBy⁡F,row,2,key=abs
H≔1−23−45−67−89
Example 4
The unsorted rtable can be updated with the sorted version:
A≔Matrix⁡16,9,5,8,13,19,7,6,17,15
A≔169581319761715
B≔SortBy⁡A,column,−1,descending,inplace
B≔131917151695876
A=A
A=131917151695876
Example 5
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
Example 6
Consider the following 3-D Array:
X≔Array⁡0.223811939491137,0.646313010111265,0.823457828327293,0.933993247757551,0.970592781760616,0.498364051982143,0.795199901137063,0.0318328463774207,0.959492426392903,0.546881519204984,0.679702676853675,0.0344460805029088,0.392227019534168,0.141886338627215,0.913375856139019,0.585267750979777,0.445586200710899,0.0971317812358475,0.849129305868777,0.157613081677548,0.118997681558377,0.765516788149002,0.706046088019609,0.792207329559554,0.278498218867048,0.276025076998578,0.950222048838355,0.743132468124916,0.800280468888800,0.126986816293506,0.340385726666133,0.489764395788231,0.0461713906311539,0.0357116785741896,0.964888535199277,0.162611735194631,0.381558457093008,0.171186687811562,0.915735525189067,0.0975404049994095,0.754686681982361,0.317099480060861,0.757740130578333,0.485375648722841,0.905791937075619,0.959743958516081,0.186872604554379,0.276922984960890,0.655740699156587,0.957506835434298,0.655098003973841,0.438744359656398,0.655477890177557,0.421761282626275,0.632359246225410,0.709364830858073,0.694828622975817,0.678735154857773,0.957166948242946,0.814723686393179
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,..
verify⁡Y,Z,Array⁡float⁡1,digits=15
true
The ArrayTools[SortBy] command was introduced in Maple 2023.
For more information on Maple 2023 changes, see Updates in Maple 2023.
See Also
sort
Download Help Document