# Step: Order

## Overview

The **Order** step sorts the table based on one or more variables.&#x20;

#### **Example starting data:**

```
/*---------+--------*
 | student | score  |
 +---------+--------+
 | jane    | 83     |
 | neal    | 35     |
 | sam     | 74     |
 | pat     | 62     |
 *---------+--------*/
```

**Example output data:**

Order by `score` descending

```
/*---------+--------*
 | student | score  |
 +---------+--------+
 | jane    | 83     |
 | sam     | 74     |
 | pat     | 62     |
 | neal    | 35     |
 *---------+--------*/
```

{% hint style="warning" %}
Ordering large tables (>1GB) across variables with a large number of unique values requires substantial memory and isn't parallelizable. Order clauses in such cases may significantly slow your transform or cause it to fail.
{% endhint %}

## Step structure

<figure><img src="/files/N1c7yBtyKSQhOUEzvGED" alt=""><figcaption></figcaption></figure>

* There will be at least one order block where you will define a variable and a sort order.
* When multiple blocks exist, the variables will be ordered and then sub-ordered in sequence.

## Input field definitions

<table><thead><tr><th width="206">Field</th><th>Definition</th></tr></thead><tbody><tr><td><strong>Order by</strong></td><td>The variable containing the values that will be sorted.</td></tr><tr><td><strong>Sort</strong></td><td>A choice of how all values in the <code>Order by</code> variable will be sorted:<br><br>ASC (nulls first)<br>ASC (nulls last)<br>DESC (nulls first)<br>DESC (nulls last)<br><br>Note that variables of the geography data type aren't sortable.</td></tr></tbody></table>

## Examples

### Example 1: Basic order

We can sort a table to quickly see the highest scores.&#x20;

#### Starting data:

```
/*---------+-------+---------+------------*
 | test    | score | student | date       |
 +---------+-------+---------+------------+
 | quiz    | 83    | jane    | 2020-04-01 |
 | quiz    | 35    | pat     | 2020-04-01 |
 | quiz    | 89    | sam     | 2020-04-01 |
 | midterm | 74    | jane    | 2020-05-01 |
 | midterm | 62    | pat     | 2020-05-01 |
 | midterm | 100   | sam     | 2020-05-01 |
 | final   | 77    | jane    | 2020-06-01 |
 | final   | 59    | pat     | 2020-06-01 |
 | final   | 100   | sam     | 2020-06-01 |
 *---------+-------+---------+------------*/
```

#### Input fields

<figure><img src="/files/Pgw17C30eNnp8NvvSdM6" alt=""><figcaption></figcaption></figure>

* **Order by:** The `score` variable has the data we want to sort on, so we select it here
* **Sort:** We want the data to go from smallest to largest values, so we choose ASC. There are no null values in this table, so we can choose either `nulls first` or `nulls last` and get the same result.

**Output data**:

```
/*---------+-------+---------+------------*
 | test    | score | student | date       |
 +---------+-------+---------+------------+
 | midterm | 100   | sam     | 2020-05-01 |
 | final   | 100   | sam     | 2020-06-01 |
 | quiz    | 89    | sam     | 2020-04-01 |
 | quiz    | 83    | jane    | 2020-04-01 |
 | final   | 77    | jane    | 2020-06-01 |
 | midterm | 74    | jane    | 2020-05-01 |
 | midterm | 62    | pat     | 2020-05-01 |
 | final   | 59    | pat     | 2020-06-01 |
 | quiz    | 35    | pat     | 2020-04-01 |
 *---------+-------+---------+------------*/
```

### Example 2: Ordering on multiple variables

Lets say instead we first wanted to sort first by year, then by the the lowest sales number.

#### Starting data:

```
/*---------+-------+---------+------------*
 | test    | score | student | date       |
 +---------+-------+---------+------------+
 | quiz    | 83    | jane    | 2020-04-01 |
 | quiz    | 35    | pat     | 2020-04-01 |
 | quiz    | 89    | sam     | 2020-04-01 |
 | midterm | 74    | jane    | 2020-05-01 |
 | midterm | 62    | pat     | 2020-05-01 |
 | midterm | 100   | sam     | 2020-05-01 |
 | final   | 77    | jane    | 2020-06-01 |
 | final   | 59    | pat     | 2020-06-01 |
 | final   | 100   | sam     | 2020-06-01 |
 *---------+-------+---------+------------*/
```

#### Input fields:

<figure><img src="/files/FmbWyTwFQjtb3E0J71zu" alt=""><figcaption></figcaption></figure>

First block

* Order by: The first variable we want the data sorted on is `score` so we choose it in the first block.
* Sort: We want the earliest information first, so we know we want the information to be ascending. This variable has a null value so it matters whether we want nulls to appear first or last in the order. Since we want it last, we choose `ASC (nulls last)`.&#x20;

Second block

* Order by: The second variable we want to sort on is `date` so we put it here.
* Sort: Since we want the most recent (highest) values first, we want it to be descending. There are no null values in this variable so where we put the nulls does not matter. We choose `DESC (nulls first)`.

#### Output data:

```
/*---------+-------+---------+------------*
 | test    | score | student | date       |
 +---------+-------+---------+------------+
 | final   | 100   | sam     | 2020-06-01 |
 | midterm | 100   | sam     | 2020-05-01 |
 | quiz    | 89    | sam     | 2020-04-01 |
 | quiz    | 83    | jane    | 2020-04-01 |
 | final   | 77    | jane    | 2020-06-01 |
 | midterm | 74    | jane    | 2020-05-01 |
 | midterm | 62    | pat     | 2020-05-01 |
 | final   | 59    | pat     | 2020-06-01 |
 | quiz    | 35    | pat     | 2020-04-01 |
 *---------+-------+---------+------------*/
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.redivis.com/reference/workflows/transforms/step-order.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
