Skip to content

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.

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.

ParameterTypeDefaultDescription
tableDataTable—The DataTable to sort.
columnstring—Name of the column to sort by.
ParameterTypeDefaultDescription
directionstring"asc"Direction: asc (ascending) · desc (descending).

Returns a new DataTable with the same columns and rows, sorted. A table with no rows gives a table with no rows.

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

Sort invoices by amount, highest first, and take the first one:

Sort DataTable table = invoices column = "Total" direction = desc → output: byAmount
Get Data Row table = byAmount index = 0 → output: highest
Log message = "Highest invoice: " + str(highest["Number"])