Sort DataTable
Sort DataTable
Section titled “Sort DataTable”Type: sort-datatable · Package: DataTable Activities v1.0.5 · Output: DataTable
Sorts a DataTable by a column and returns a new sorted table. Useful to present data in a logical order (by date, by amount), process the most urgent items first, or prepare a report.
How it works
Section titled “How it works”Reorders the rows by the values of the given column, in ascending (asc, default) or descending (desc) order. It returns a new table with the same columns; the original does not change.
The order depends on the column type: a numeric or date column is sorted by value, and a text column alphabetically, case-insensitively. Note that in a text column numbers are sorted as text ("10" before "9"); if your data comes as text, first create a numeric column with Add Column (num(Total)) and sort by it. Empty cells come first in ascending order.
Required parameters
Section titled “Required parameters”| Parameter | Type | Default | Description |
|---|---|---|---|
table | DataTable | — | The DataTable to sort. |
column | string | — | Name of the column to sort by. |
Optional parameters
Section titled “Optional parameters”| Parameter | Type | Default | Description |
|---|---|---|---|
direction | string | "asc" | Direction: asc (ascending) · desc (descending). |
Output
Section titled “Output”Returns a new DataTable with the same columns and rows, sorted. A table with no rows gives a table with no rows.
Common errors
Section titled “Common errors”| Message | Cause and fix |
|---|---|
'table' must be a DataTable. | table does not evaluate to a DataTable. |
'column' is required. | The column is missing. |
Invalid 'direction' '…'. Use asc or desc. | direction has another value. |
Cannot find column [….] | The column does not exist (.NET message). Check the name. |
Example
Section titled “Example”Sort invoices by amount, highest first, and take the first one:
Sort DataTable table = invoices column = "Total" direction = desc → output: byAmountGet Data Row table = byAmount index = 0 → output: highestLog message = "Highest invoice: " + str(highest["Number"])Related activities
Section titled “Related activities”- Filter DataTable — filter before sorting.
- Group By — group and summarize.