---
title: Sort
---

> [!WARNING]
> Legacy documentation: This documentation applies to **Legacy Deephaven Enterprise only** and does not apply to Core+.

> [!NOTE]
> For Core+ workers, see Community Core documentation for [sort](/core/docs/reference/table-operations/sort/sort).

Sorting is a way to organize and view your data. You can sort any table by clicking on a column header or accessing the column header context menu. Here, we show how to change sort order in a query.

### `sort()`

The `sort` method rearranges rows in a table by ascending (smallest to largest) order based on the column(s) listed in the `columnsToSortBy` argument:

```
.sort("ColumnsToSortBy")
```

![img](../../assets/coreops/sortStockTrades.png)

For example, the following query orders the Exchange column from A-Z.

```python test-set=1 skip-test
t=db.t("LearnDeephaven","StockTrades")\
.where("Date = `2017-08-25`)\
.sort("Exchange")
```

The `sort` method can also be applied on multiple columns. In this example, we will sort first on the Exchange column and then on the Last column.

Here's the query:

```python
t2 = (
    db.t("LearnDeephaven", "StockTrades")
    .where("Date = `2017-08-25`")
    .sort("Exchange", "Last")
)
```

![img](../../assets/coreops/sortontwocolumns.png)

The `sortDescending` method rearranges rows in a table by descending (largest to smallest) order based on the column(s) listed in the `columnsToSortBy` argument:

```
.sortDescending("ColumnsToSortBy")
```

The `sortDescending` method rearranges rows in a table by descending (largest to smallest) order based on the column(s) listed in the `columnsToSortBy` argument.

In the next example, we'll sort StockTrades in descending order.

Here's the query:

```python
t3 = (
    db.t("LearnDeephaven", "StockTrades")
    .where("Date = `2017-08-25`")
    .sortDescending("Exchange", "Last")
)
```

![img](../../assets/coreops/sortdescending.png)

### `SortPair`

Sort Pairs apply sort orders on different columns within one sort call to reduce computing expense. Using the StockTrades table once again, we can create a Sort Pair that arranges the Exchange column in ascending order, and arranges the Last column in descending order.

> [!NOTE]
> The `SortPair` methods require that the appropriate package be imported into Deephaven.

Here's the query:

```python
from deephaven import SortPair

t4 = (
    db.t("LearnDeephaven", "StockTrades")
    .where("Date = `2017-08-25`")
    .sort(SortPair.ascending("Exchange"), SortPair.descending("Last"))
)
```

```groovy
import com.illumon.iris.db.tables.SortPair

t4 = db.t("LearnDeephaven" , "StockTrades")\
    .where("Date = `2017-08-25`")\
    .sort(SortPair.ascending("Exchange"), SortPair.descending("Last"))
```

The following methods are also available:

- `SortPair.ascendingPairs("colA", "colB", "colC")` - This will sort all of the specified columns in ascending order.
- `SortPair.descendingPairs("colA", "colB", "colC")` - This will sort all of the specified columns in descending order.

Compare the following two queries:

```groovy test-set=1 skip-test
verbose = t.sort(
SortPair.descending("ChangePct"),
SortPair.ascending("Expiry"),
SortPair.descending("OptionVolume")
)
```

```groovy test-set=1 skip-test
concise = t.sort(
SortPair.descendingPairs("ChangePct", "OptionVolume"),
SortPair.ascending("Expiry")
)
```

The results are equivalent, however, the second query is more concise.

## Restricting Table Sorting

Sorting can be a very expensive operation in database applications in terms of processing power, time, and memory, especially for tables with millions or billions of rows. When many users access and sort the same large table at the same time, the efficiency of the system can be compromised, resulting in slow response times or program failure. Therefore, it can become necessary to add sorting restrictions to such tables, or to limit access to the table until its size has been reduced.

Sorting restrictions can be applied to tables by using the `restrictSortTo` method:

```
.restrictSortTo("<Col1>", "<Col2>",...)
```

The arguments to the restrictSortTo method are the name(s) of the columns in the table where sorting will be allowed. Sorting in any other columns in the table will not be available.

For example, the following example adds sorting restrictions to the StockTrades table.

```python
t5 = (
    db.t("LearnDeephaven", "StockTrades")
    .where("Date=`2017-08-25`")
    .restrictSortTo("Exchange", "Sym")
)
```

As you can see below, the clicking once on the Exchange column sorts its rows in ascending order, while clicking on the Last column produces no change.

![img](../../assets/coreops/restrictsort.gif)

Sorting restrictions are transitive, meaning the restrictions continue to be enforced as subsequent tables are generated. However, new table objects are not created when using the `restrictSortTo` operation. To create a new table object after applying sort restrictions, you must assign a new variable to hold the new table object and apply an operation such as `select`, `view`, `updateView`, or `where`. Alternatively, you can also wait to apply the sorting restrictions until the end of the query.

Sorting restrictions can be removed from a table using the `clearSortingRestrictions` method:

```
clearSortingRestrictions()
```

Using this method removes all sorting restrictions for the table. For example, the following query first restricts sorting in the table named "tRestrict", and then removes all sorting restrictions in the table named "tUnrestrict":

```python
t = db.t("LearnDeephaven", "StockTrades").where("Date=`2017-08-25`")
tRestrict = t.restrictSortTo("Exchange", "Sym")
tUnrestrict = (
    tRestrict.where("Exchange=`Nyse`")
    .updateView("Symbol=Sym")
    .clearSortingRestrictions()
)
```

## Reversing Table Order

Users may want to reverse the order of the rows in an entire table, where the last row becomes the first row and vice versa. Deephaven offers two ways to accomplish this: by using the `reverse` method in a query, or through the Deephaven console.

> [!NOTE]
> See
> [Working with Tables > Reversing Table Order](../../interfaces/web/working-with-tables.md#reverse-table-order)

The `reverse` method can be used in a query to reverse an entire Deephaven table. An example of this follows:

```python
t = db.t("LearnDeephaven", "StockTrades").where("Date=`2017-08-25`")
t6 = t.reverse()
```

The following image shows the first 10 rows in the `t` table:

![img](../../assets/coreops/first10.png)

The next image demonstrates how these rows are now presented as the last ten rows in the `t6` table:

![img](../../assets/coreops/last10.png)
