--- title: "system.dataset.sort" description: "Sorts a dataset and returns the sorted dataset." --- # system.dataset.sort > Sorts a dataset and returns the sorted dataset. This function is used in **Python Scripting**. ## Description Takes a [dataset](platform/scripting/python-scripting/variables-datatypes-and-objects/datasets.md) 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](platform/scripting/python-scripting/variables-datatypes-and-objects/datasets.md#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 Values|Sorted - Ascending|Sorted - Descending| |---|---|---| |a1|a1|Z3| |a22|A1|z3| |Z3|a4|a77z99| |z3|a7z9|a77z4| |a4|a22|a22| |a77z4|a77z4|a7z9| |a77z99|a77z99|a4| |a7z9|Z3|a1| |A1|z3|A1| ### Natural Ordering The naturalOrdering parameter allows the function to sort using either a natural ordering, or an alphabetical ordering. |Raw Column Values|Natural Ordering - Ascending|Alphabetical Ordering - Ascending| |---|---|---| |a11|a1|a1| |a2|a2|a11| |a1|a11|a2| ### Client Permission Restrictions This scripting function has no [Client Permission](ignition-modules\vision\vision-project-properties\vision-project-properties.md#vision-permissions-properties) restrictions. ## Syntax `system.dataset.sort(dataset, keyColumn [, ascending, naturalOrdering])` ### Parameters Type | Parameter | Description ------- | ------- | ------- Dataset | `dataset` | The dataset to sort. Integer / String | `keyColumn` | The index of the column to sort on. Boolean | `ascending` | True for ascending order, False for descending order. If omitted, ascending order will be used. [optional] Boolean | `naturalOrdering` | True 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 ```python title="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 ``` ```python title="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 ```