sortByColumn
Sorts a table region by the values in one column.
The sort is performed using a comparator function or lambda expression.
Studio has other sorting functions, some of which take fewer parameters. To perform a single-column sort that uses numeric or string values, consider using one of these functions instead:
We recommend using sortByColumn
to perform a single-column sort when none of the functions listed above are suitable. One example is when you need to sort by a single column of datetime values. This is shown below.
Note
Available in "Describe the Table" and "Describe Row in Context" projects only.
Parameters
INPUT TABLE (table region)
The table region to sort.
SORTING COLUMN (string)
The column to sort by. Give the column name as a string.
COMPARATOR FUNCTION (function)
A comparator function that defines how to sort the values.
Use a predefined comparator function or lambda expression.
Comparator functions
Name | Description | Equivalent Lambda Expression |
---|---|---|
| Sorts numbers in ascending order. |
|
| Sorts numbers in descending order. |
|
| Sorts strings in alphabetical order. |
|
| Sorts strings in reverse-alphabetical order. |
|
Examples
Assume the following table data:
| ID | Date | Sales |
---|---|---|---|
Row 1 | 1001 | 2022-01-13 | 478,745.37 |
Row 2 | 1002 | 2022-01-30 | 329,493.49 |
Row 3 | 1003 | 2022-01-02 | 467,359.45 |
Row 4 | 1004 | 2022-01-21 | 463,603.17 |
Note
The first table column — ID in table above — contains the row names.
To sort the table in chronological order, using the values in the Date column:
[[sortByColumn(WholeTable, 'Date', (a,b) -> b < a ? 1 : (b == a ? 0 : -1))]]
The result is this sorted table region:
Date | Sales | |
---|---|---|
1003 | 2022-01-02 | 467,359.45 |
1001 | 2022-01-13 | 478,745.37 |
1004 | 2022-01-21 | 463,603.17 |
1002 | 2022-01-30 | 329,493.49 |
To sort the region in reverse-chronological order, using the values in the Date column:
[[sortByColumn(WholeTable, 'Date', (a,b) -> b > a ? 1 : (b == a ? 0 : -1))]]
The result is this sorted table region:
Date | Sales | |
---|---|---|
1002 | 2022-01-30 | 329,493.49 |
1004 | 2022-01-21 | 463,603.17 |
1001 | 2022-01-13 | 478,745.37 |
1003 | 2022-01-02 | 467,359.45 |