Skip to main content
Version: Python

Sort table data

Sorting is a common operation in data analysis, and Deephaven makes it easy to sort data in a variety of ways. This guide will show you how to sort table data programmatically.

tip

You can also sort columns by right-clicking the column header in the UI.

sort and sort_descending

You can sort table data programmatically by using the sort() and sort_descending methods:

from deephaven import new_table
from deephaven.column import string_col, int_col

source = new_table(
[
string_col("Letter", ["A", "B", "A", "B", "B", "A"]),
int_col("Number", [6, 6, 1, 3, 4, 4]),
string_col("Color", ["red", "blue", "orange", "purple", "yellow", "pink"]),
]
)

result_sort = source.sort(order_by=["Letter"])
result_sort_desc = source.sort_descending(order_by=["Letter"])

Given a column name to order the data by, sort returns a new table with the data sorted in ascending (A-Z) order, and sort_descending returns a new table with the data sorted in descending (Z-A) order.

Sorting by multiple columns

Both sort and sort_descending can sort multiple columns at once. For example, here we will sort the table from the previous example by our Letter column, then the Number column.

result_sort = source.sort(order_by=["Letter", "Number"])
result_sort_desc = source.sort_descending(order_by=["Letter", "Number"])

Complex sorts

The sort method can be used to sort multiple columns in different directions. For example, we can sort by Letter in ascending order, then by Number in descending order.

from deephaven import SortDirection

asc = SortDirection.ASCENDING
desc = SortDirection.DESCENDING

result = source.sort(order_by=["Letter", "Number"], order=[asc, desc])

This is simpler than invoking both methods to accomplish the same result:

result = source.sort(order_by="Letter").sort_descending(order_by="Number")

restrict_sort_to

The restrict_sort_to method allows you to restrict the columns that can be sorted via the UI. This is useful if you want to prevent yourself or other users from accidentally performing expensive sort operations as you interact with tables in the UI. For example, we can restrict sorting to the Letter column:

table = source.restrict_sort_to(cols="Letter")

Now, we can still sort by Letter, but attempting to sort by Number will result in an error:

t_sorted = table.sort(order_by="Number")