sortDataset
This function is used by Ignition's Expression language.
Description​
Takes a dataset and returns a sorted version of dataset. The sort order is determined by a single column. This works on numeric, as well as alphanumeric columns. When sorting alphanumerically, contiguous numbers are treated as a single number: you may recognize this as a "natural sort".
Sort Order​
The table below represents an example of how alphanumeric values are sorted by the function (assuming a natural sort). Where Raw Column Values represents an initial set of values, and the Sorted columns show how the function sorts in Ascending and Descending order.
Raw Column Values | Sorted - Ascending | Sorted - Descending |
---|---|---|
a1 | a1 | Z3 |
A1 | Z3 | A1 |
a4 | a22 | a22 |
a7z9 | z3 | a1 |
a22 | A1 | z3 |
a77z4 | a77z4 | a7z9 |
a77z99 | a77z99 | a4 |
Z3 | a4 | a77z99 |
z3 | a7z9 | a77z4 |
Some caveats to be aware of:
- Null values for string columns are sorted first
- Null values for numeric columns are sorted last
- Casing is not used as a method of sorting. If the only difference between two cells is the casing, then the resulting order depends largely on where the cells were in the raw column.
Syntax (index)​
sortDataset(dataset, colIndex, [ascending], [naturalOrdering])
Parameters
DataSet dataset - The starting dataset.
Integer colIndex - The index of the column to sort on.
Boolean ascending - A flag indicating whether or not to sort ascending. Defaults to true. [optional]
Boolean naturalOrdering - A flag indicating the ordering method. True for natural, false for alphabetical. Defaults to true. [optional]
Results
- DataSet - A sorted dataset
Syntax (name)​
sortDataset(dataset, colName, [ascending], [naturalOrdering])
Parameters
DataSet dataset - The starting dataset.
String colName- The name of the column to sort on.
Boolean ascending - A flag indicating whether or not to sort ascending. Defaults to true. [optional]
Boolean naturalOrdering - A flag indicating the ordering method. True for natural, false for alphabetical. Defaults to true. [optional]
Results
- DataSet - A sorted dataset
Examples​
sortDataset(dataset, 0, true) // returns a Dataset sorted ascending on column 0.
sortDataset(dataset, "Column 1", false) // returns a Dataset sorted descending on the column named "Column 1".