SortBy - Maple Help

ArrayTools

 SortBy
 sort rtable by row, column, or other 1-D slice

 Calling Sequence SortBy( rtable1D, options ) SortBy( rtable2D, column_or_row, index, options ) SortBy( rtableND, Indices, options )

Parameters

 rtable1D - A non-empty 1-D rtable of data rtable2D - A non-empty 2-D rtable of data rtableND - A non-empty rtable of data having one or more dimensions column_or_row - Either column or row, specifies if a column or row will define 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

Options

 • 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.

Description

 • The $\mathrm{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, $\mathrm{SortBy}\left(A,\mathrm{column},3\right)$ 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 $\mathrm{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 $\mathrm{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, $\mathrm{SortBy}\left(A,\left[..\right]\right)$ is equivalent to $\mathrm{SortBy}\left(A\right)$.
 • For the 2-D case, $\mathrm{SortBy}\left(A,\left[i,..\right]\right)$ is equivalent to $\mathrm{SortBy}\left(A,\mathrm{row},i\right)$ and $\mathrm{SortBy}\left(A,\mathrm{column},j\right)$ is equivalent to $\mathrm{SortBy}\left(A,\left[..,j\right]\right)$.
 • 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.

Examples

 > $\mathrm{with}\left(\mathrm{ArrayTools}\right):$

Example 1

 > $A≔{\mathrm{Vector}}_{'\mathrm{row}'}\left(\left[9,0,16,4,1\right]\right)$
 ${A}{≔}\left[\begin{array}{ccccc}{9}& {0}& {16}& {4}& {1}\end{array}\right]$ (1)
 > $\mathrm{SortBy}\left(A\right)$
 $\left[\begin{array}{ccccc}{0}& {1}& {4}& {9}& {16}\end{array}\right]$ (2)

Example 2

 > $A≔\mathrm{Matrix}\left(\left[\left[8,18,9\right],\left[3,16,7\right],\left[11,1,15\right],\left[4,13,12\right]\right]\right)$
 ${A}{≔}\left[\begin{array}{ccc}{8}& {18}& {9}\\ {3}& {16}& {7}\\ {11}& {1}& {15}\\ {4}& {13}& {12}\end{array}\right]$ (3)
 > $\mathrm{SortBy}\left(A,'\mathrm{column}',2\right)$
 $\left[\begin{array}{ccc}{11}& {1}& {15}\\ {4}& {13}& {12}\\ {3}& {16}& {7}\\ {8}& {18}& {9}\end{array}\right]$ (4)
 > $\mathrm{SortBy}\left(A,'\mathrm{row}',-1\right)$
 $\left[\begin{array}{ccc}{8}& {9}& {18}\\ {3}& {7}& {16}\\ {11}& {15}& {1}\\ {4}& {12}& {13}\end{array}\right]$ (5)

Example 3

 • Custom sorting options can be passed:
 > $F≔\mathrm{Matrix}\left(\left[\left[1,-2,3\right],\left[-4,5,-6\right],\left[7,-8,9\right]\right]\right)$
 ${F}{≔}\left[\begin{array}{ccc}{1}& {-2}& {3}\\ {-4}& {5}& {-6}\\ {7}& {-8}& {9}\end{array}\right]$ (6)
 > $G≔\mathrm{SortBy}\left(F,'\mathrm{row}',2\right)$
 ${G}{≔}\left[\begin{array}{ccc}{3}& {1}& {-2}\\ {-6}& {-4}& {5}\\ {9}& {7}& {-8}\end{array}\right]$ (7)
 > $H≔\mathrm{SortBy}\left(F,'\mathrm{row}',2,'\mathrm{key}'=\mathrm{abs}\right)$
 ${H}{≔}\left[\begin{array}{ccc}{1}& {-2}& {3}\\ {-4}& {5}& {-6}\\ {7}& {-8}& {9}\end{array}\right]$ (8)

Example 4

 • The unsorted rtable can be updated with the sorted version:
 > $A≔\mathrm{Matrix}\left(\left[\left[16,9\right],\left[5,8\right],\left[13,19\right],\left[7,6\right],\left[17,15\right]\right]\right)$
 ${A}{≔}\left[\begin{array}{cc}{16}& {9}\\ {5}& {8}\\ {13}& {19}\\ {7}& {6}\\ {17}& {15}\end{array}\right]$ (9)
 > $B≔\mathrm{SortBy}\left(A,'\mathrm{column}',-1,'\mathrm{descending}','\mathrm{inplace}'\right)$
 ${B}{≔}\left[\begin{array}{cc}{13}& {19}\\ {17}& {15}\\ {16}& {9}\\ {5}& {8}\\ {7}& {6}\end{array}\right]$ (10)
 > $'A'=A$
 ${A}{=}\left[\begin{array}{cc}{13}& {19}\\ {17}& {15}\\ {16}& {9}\\ {5}& {8}\\ {7}& {6}\end{array}\right]$ (11)

Example 5

 • Arrays with starting indices different than $1$ are accepted:
 > $A≔\mathrm{Array}\left(0..2,0..4,\left[\left[4,15,8,2,18\right],\left[5,18,2,3,17\right],\left[11,15,20,7,1\right]\right]\right)$
 • For this Array, "row 1" refers to what would be "row 2" if the starting indices were shifted to all be $1$:
 > $B≔\mathrm{SortBy}\left(A,'\mathrm{row}',1\right)$

Example 6

 • Consider the following 3-D Array:
 > $X≔\mathrm{Array}\left(\left[\left[\left[0.223811939491137,0.646313010111265,0.823457828327293,0.933993247757551,0.970592781760616\right],\left[0.498364051982143,0.795199901137063,0.0318328463774207,0.959492426392903,0.546881519204984\right],\left[0.679702676853675,0.0344460805029088,0.392227019534168,0.141886338627215,0.913375856139019\right]\right],\left[\left[0.585267750979777,0.445586200710899,0.0971317812358475,0.849129305868777,0.157613081677548\right],\left[0.118997681558377,0.765516788149002,0.706046088019609,0.792207329559554,0.278498218867048\right],\left[0.276025076998578,0.950222048838355,0.743132468124916,0.800280468888800,0.126986816293506\right]\right],\left[\left[0.340385726666133,0.489764395788231,0.0461713906311539,0.0357116785741896,0.964888535199277\right],\left[0.162611735194631,0.381558457093008,0.171186687811562,0.915735525189067,0.0975404049994095\right],\left[0.754686681982361,0.317099480060861,0.757740130578333,0.485375648722841,0.905791937075619\right]\right],\left[\left[0.959743958516081,0.186872604554379,0.276922984960890,0.655740699156587,0.957506835434298\right],\left[0.655098003973841,0.438744359656398,0.655477890177557,0.421761282626275,0.632359246225410\right],\left[0.709364830858073,0.694828622975817,0.678735154857773,0.957166948242946,0.814723686393179\right]\right]\right]\right)$
 • 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≔\mathrm{SortBy}\left(X,\left[2,\left(\right)..\left(\right),1\right],'\mathrm{descending}','\mathrm{output}'=\left['\mathrm{permutation}','\mathrm{sorted}'\right]\right)$
 • The sorted Array $Y$ can be reproduced using this permutation $P$:
 > $Z≔{X}_{\left(\right)..\left(\right),P,\left(\right)..\left(\right)}$
 > $\mathrm{verify}\left(Y,Z,'\mathrm{Array}\left(\mathrm{float}\left(1,\mathrm{digits}=15\right)\right)'\right)$
 ${\mathrm{true}}$ (12)

Compatibility

 • The ArrayTools[SortBy] command was introduced in Maple 2023.