 Sort - Maple Help

# Online Help

###### All Products    Maple    MapleSim

DataFrame/sort

sort a DataFrame by values in a given column Calling Sequence sort( DF, index ) sort( DF, index, ordering, options ) Parameters

 DF - a DataFrame object index - name, string or integer value; specifies the column index for the chosen column ordering - symbol or Boolean function of two arguments; sort ordering options - (optional) equation(s) of the form option=value Options

 • nonstrict: specifies to use a nonstrict comparison function type
 • strict: specifies to use a strict comparison function type
 • key: specifies to use a key comparison function type
 • output: sorted or permutation; specify whether to return the sorted DataFrame or the permutation of rows that sorts the DataFrame accordingly Description

 • The sort command sorts the rows of a DataFrame according to the sorted values in a given column.
 • If the ordering option is given, it specifies the ordering for sorting elements. By default, elements are sorted in ascending order for numeric values and lexicographic order for strings and names.
 – <:  If ordering is the symbol < or numeric, then the DataFrame is sorted in ascending numerical order of values in a given column. The index column of the DataFrame must contain elements of type({numeric, real_infinity}),
 – >:  If ordering is the symbol >, then the DataFrame is sorted into descending numerical order.
 – address:  If ordering 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 ordering is the symbol length, then the elements are sorted by length where length is as determined by the length function.
 – lexorder:  If ordering is the symbol lexorder or string, then lists of strings or symbols are sorted into lexicographic order.
 – Otherwise, ordering must be a Boolean-valued function, F, of two arguments.  Specifically, $F\left(a,b\right)$ returns $\mathrm{false}$ if and only if $b$ must precede $a$ in the sorted output.  That is $F\left(a,b\right)$ is a non-strict less than comparison function.  In addition, $F\left(a,b\right)$ must be defined for all pairs $a,b$ for $a$ and $b$ in the input structure and $F\left(a,b\right)$ must be transitive, that is, if $F\left(a,b\right)=\mathrm{true}$ and $F\left(b,c\right)=\mathrm{true}$ then $F\left(a,c\right)=\mathrm{true}$.
 • By specifying comptype=Function, different styles of comparison functions can be given to sort.  The supported values for comptype are:
 – nonstrict: the comparison Function is a non-strict less than function, as described above.
 – strict: the comparison Function is a strict less than function.  That is $F\left(a,b\right)$ returns $\mathrm{true}$ if and only if $a$ must precede $b$ in the sorted output.  Function 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: the Function maps each element in the DataFrame to a key value.  The DataFrame is sorted by sorting the corresponding keys.  Using a key function is preferable to a comparison function because the key function is called $\mathrm{O}\left(n\right)$ times, whereas a comparison function will be called $\mathrm{O}\left(n\mathrm{log}\left(n\right)\right)$ 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 an argument output = sorted is supplied, sort returns the sorted argument. This is the default behavior.
 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 DataFrame would occur at the ith position in the sorted argument. This means that if $a=\mathrm{sort}\left(\mathrm{DataFrame},\mathrm{ByColumn},'\mathrm{output}=\mathrm{permutation}'\right)$ then $\mathrm{sort}\left(\mathrm{DataFrame}\right)$ could be obtained as ${\mathrm{DataFrame}}_{\left[a\right],..}$.
 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. Examples

Consider some data on berries.

 > $\mathrm{genus}≔⟨"Rubus","Vitis","Fragaria"⟩:$
 > $\mathrm{energy}≔⟨220,288,136⟩:$
 > $\mathrm{carbohydrates}≔⟨11.94,18.1,7.68⟩:$
 > $\mathrm{top_producer}≔⟨\mathrm{Russia},\mathrm{China},\mathrm{USA}⟩:$
 > $\mathrm{berries}≔\mathrm{DataFrame}\left(⟨\mathrm{energy}|\mathrm{carbohydrates}|\mathrm{top_producer}|\mathrm{genus}⟩,\mathrm{columns}=\left[\mathrm{Energy},\mathrm{Carbohydrates},\mathrm{Top Producer},\mathrm{Genus}\right],\mathrm{rows}=\left[\mathrm{Raspberry},\mathrm{Grape},\mathrm{Strawberry}\right]\right)$
 ${\mathrm{berries}}{≔}\left[\begin{array}{ccccc}{}& {\mathrm{Energy}}& {\mathrm{Carbohydrates}}& {\mathrm{Top Producer}}& {\mathrm{Genus}}\\ {\mathrm{Raspberry}}& {220}& {11.94}& {\mathrm{Russia}}& {"Rubus"}\\ {\mathrm{Grape}}& {288}& {18.1}& {\mathrm{China}}& {"Vitis"}\\ {\mathrm{Strawberry}}& {136}& {7.68}& {\mathrm{USA}}& {"Fragaria"}\end{array}\right]$ (1)

It is possible to sort by any column. Here the DataFrame is sorted in order of ascending energy level:

 > $\mathrm{sort}\left(\mathrm{berries},\mathrm{Energy}\right)$
 $\left[\begin{array}{ccccc}{}& {\mathrm{Energy}}& {\mathrm{Carbohydrates}}& {\mathrm{Top Producer}}& {\mathrm{Genus}}\\ {\mathrm{Strawberry}}& {136}& {7.68}& {\mathrm{USA}}& {"Fragaria"}\\ {\mathrm{Raspberry}}& {220}& {11.94}& {\mathrm{Russia}}& {"Rubus"}\\ {\mathrm{Grape}}& {288}& {18.1}& {\mathrm{China}}& {"Vitis"}\end{array}\right]$ (2)

In order of descending carbohydrate level:

 > $\mathrm{sort}\left(\mathrm{berries},\mathrm{Carbohydrates},\mathrm{>}\right)$
 $\left[\begin{array}{ccccc}{}& {\mathrm{Energy}}& {\mathrm{Carbohydrates}}& {\mathrm{Top Producer}}& {\mathrm{Genus}}\\ {\mathrm{Grape}}& {288}& {18.1}& {\mathrm{China}}& {"Vitis"}\\ {\mathrm{Raspberry}}& {220}& {11.94}& {\mathrm{Russia}}& {"Rubus"}\\ {\mathrm{Strawberry}}& {136}& {7.68}& {\mathrm{USA}}& {"Fragaria"}\end{array}\right]$ (3)

It is also possible to sort columns with string and named values:

 > $\mathrm{sort}\left(\mathrm{berries},\mathrm{Genus}\right)$
 $\left[\begin{array}{ccccc}{}& {\mathrm{Energy}}& {\mathrm{Carbohydrates}}& {\mathrm{Top Producer}}& {\mathrm{Genus}}\\ {\mathrm{Strawberry}}& {136}& {7.68}& {\mathrm{USA}}& {"Fragaria"}\\ {\mathrm{Raspberry}}& {220}& {11.94}& {\mathrm{Russia}}& {"Rubus"}\\ {\mathrm{Grape}}& {288}& {18.1}& {\mathrm{China}}& {"Vitis"}\end{array}\right]$ (4)
 > $\mathrm{sort}\left(\mathrm{berries},\mathrm{Top Producer},\mathrm{length}\right)$
 $\left[\begin{array}{ccccc}{}& {\mathrm{Energy}}& {\mathrm{Carbohydrates}}& {\mathrm{Top Producer}}& {\mathrm{Genus}}\\ {\mathrm{Strawberry}}& {136}& {7.68}& {\mathrm{USA}}& {"Fragaria"}\\ {\mathrm{Grape}}& {288}& {18.1}& {\mathrm{China}}& {"Vitis"}\\ {\mathrm{Raspberry}}& {220}& {11.94}& {\mathrm{Russia}}& {"Rubus"}\end{array}\right]$ (5)

The output option controls the returned output for the sort command. Using permutation returns the list of numeric indices that sort the original DataFrame.

 > $\mathrm{index_order}≔\mathrm{sort}\left(\mathrm{berries},\mathrm{Energy},\mathrm{>},\mathrm{output}=\mathrm{permutation}\right)$
 ${\mathrm{index_order}}{≔}\left[{2}{,}{1}{,}{3}\right]$ (6)
 > $\mathrm{berries}\left[\mathrm{index_order},..\right]$
 $\left[\begin{array}{ccccc}{}& {\mathrm{Energy}}& {\mathrm{Carbohydrates}}& {\mathrm{Top Producer}}& {\mathrm{Genus}}\\ {\mathrm{Grape}}& {288}& {18.1}& {\mathrm{China}}& {"Vitis"}\\ {\mathrm{Raspberry}}& {220}& {11.94}& {\mathrm{Russia}}& {"Rubus"}\\ {\mathrm{Strawberry}}& {136}& {7.68}& {\mathrm{USA}}& {"Fragaria"}\end{array}\right]$ (7)

Several other sort options are also supported by the DataFrame sort command:

 > $\mathrm{berries}≔\mathrm{Append}\left(\mathrm{berries},\mathrm{DataSeries}\left(⟨1,3,-2⟩,\mathrm{labels}=\left[\mathrm{Raspberry},\mathrm{Grape},\mathrm{Strawberry}\right]\right),\mathrm{label}=\mathrm{RandomValues}\right)$
 ${\mathrm{berries}}{≔}\left[\begin{array}{cccccc}{}& {\mathrm{Energy}}& {\mathrm{Carbohydrates}}& {\mathrm{Top Producer}}& {\mathrm{Genus}}& {\mathrm{RandomValues}}\\ {\mathrm{Raspberry}}& {220}& {11.94}& {\mathrm{Russia}}& {"Rubus"}& {1}\\ {\mathrm{Grape}}& {288}& {18.1}& {\mathrm{China}}& {"Vitis"}& {3}\\ {\mathrm{Strawberry}}& {136}& {7.68}& {\mathrm{USA}}& {"Fragaria"}& {-2}\end{array}\right]$ (8)
 > $\mathrm{sort}\left(\mathrm{berries},\mathrm{RandomValues},\mathrm{key}=\mathrm{abs}\right)$
 $\left[\begin{array}{cccccc}{}& {\mathrm{Energy}}& {\mathrm{Carbohydrates}}& {\mathrm{Top Producer}}& {\mathrm{Genus}}& {\mathrm{RandomValues}}\\ {\mathrm{Raspberry}}& {220}& {11.94}& {\mathrm{Russia}}& {"Rubus"}& {1}\\ {\mathrm{Strawberry}}& {136}& {7.68}& {\mathrm{USA}}& {"Fragaria"}& {-2}\\ {\mathrm{Grape}}& {288}& {18.1}& {\mathrm{China}}& {"Vitis"}& {3}\end{array}\right]$ (9) Compatibility

 • The DataFrame/sort command was introduced in Maple 2017.
 • For more information on Maple 2017 changes, see Updates in Maple 2017.

 See Also