Skip to main content
Version: 8.1

system.dataset.sort

This function is used in Python Scripting.

Description

Takes a dataset and returns a sorted version of the 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".

note

Datasets are immutable, which means they cannot be directly modified once created. Instead, this scripting function returns a new dataset with some modification applied, which must be assigned to a variable to be used. See Altering a Dataset.

Alphanumeric Sort

The table below represents an example of how alphanumeric values are sorted by the function. Raw Column Values represents the initial set of values in a column. The Sorted columns show how the function sorts in Ascending and Descending order.

Raw Column ValuesSorted - AscendingSorted - Descending
a1a1Z3
a22A1z3
Z3a4a77z99
z3a7z9a77z4
a4a22a22
a77z4a77z4a7z9
a77z99a77z99a4
a7z9Z3a1
A1z3A1

Natural Ordering

The naturalOrdering parameter allows the function to sort using either a natural ordering, or an alphabetical ordering.

Raw Column ValuesNatural Ordering - AscendingAlphabetical Ordering - Ascending
a11a1a1
a2a2a11
a1a11a2

Client Permission Restrictions

This scripting function has no Client Permission restrictions.

Syntax

system.dataset.sort(dataset, keyColumn [, ascending, naturalOrdering])

Parameters

TypeParameterDescription
DatasetdatasetThe dataset to sort.
Integer / StringkeyColumnThe index of the column to sort on.
BooleanascendingTrue for ascending order, False for descending order. If omitted, ascending order will be used. [optional]
BooleannaturalOrderingTrue for natural ordering, False for alphabetical ordering. Ignored if the sort column is a directly sortable data type. If omitted, defaults to True (natural ordering). [optional]

Returns

Dataset - A new sorted dataset.

Scope

Gateway, Vision Client, Perspective Session

Code Examples

Code Snippet - Sorting a Dataset in a Vision Table Component
# This code will take the data in a Vision Table component, sort it based on the column with index 1,
# and then reinsert the sorted data into the same Table.

data = event.source.parent.getComponent('Table').data
newData = system.dataset.sort(data, 1)
event.source.parent.getComponent('Table').data = newData
Code Snippet - Initializing and Sorting a Dataset by Column Name
# This code will create a dataset in scripting, and then sort it based on the name of one of the columns.
# It then inserts the sorted dataset into a table component.

# Initialize column headers and empty data list
headers = ["City", "Population", "Timezone", "GMTOffset"]
data = []
# Add rows, one by one, into data list
data.append(["New York", 8363710, "EST", -5])
data.append(["Los Angeles", 3833995, "PST", -8])
data.append(["Chicago", 2853114, "CST", -6])
data.append(["Houston", 2242193, "CST", -6])
data.append(["Phoenix", 1567924, "MST", -7])
# Convert headers and data lists into dataset
cities = system.dataset.toDataSet(headers, data)
# Sort the resulting dataset by city name
newData = system.dataset.sort(cities, "City")
# Write final dataset to a table
event.source.parent.getComponent('Table').data = newData

Keywords

system dataset sort, dataset.sort