

# List sorting and reversing functions

Use these functions to sort a list in ascending or descending order, to reverse its order, or to get the positions of its elements in sorted order. When you sort, you can choose whether `NULL` elements come first or last.

## `array_reverse` function
<a name="athena-duckdb-functions-list-array-reverse"></a>

Returns the elements of `list` in reverse order. `NULL` elements are kept and move with the other elements. An empty list returns an empty list, and if `list` is `NULL`, the result is `NULL`.

For a list of lists, only the outer list is reversed, and each inner list keeps its order. If you pass an `ARRAY` value, the result is a `LIST`. `array_reverse` returns the same result as `list_reverse`.

### Syntax
<a name="athena-duckdb-functions-list-array-reverse-syntax"></a>

```sql
array_reverse({{list}})
```

### Arguments
<a name="athena-duckdb-functions-list-array-reverse-arguments"></a>

The `array_reverse` function takes the following argument:

**{{list}}**  
The list to reverse.

### Return type
<a name="athena-duckdb-functions-list-array-reverse-return-type"></a>

A list of the element type of `list`, such as `INTEGER[]`.

### Examples
<a name="athena-duckdb-functions-list-array-reverse-examples"></a>

The following SQL query reverses a list.

```sql
SELECT array_reverse([4, 5, 6]) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[6, 5, 4]` | 

The following SQL query reverses a list of strings that has a `NULL` element.

```sql
SELECT array_reverse(['a', NULL, 'c']) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[c, NULL, a]` | 

The following SQL query reverses a list of lists. The inner lists keep their order.

```sql
SELECT array_reverse([[1, 2], [3]]) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[[3], [1, 2]]` | 

### Considerations
<a name="athena-duckdb-functions-list-array-reverse-considerations"></a>

`array_reverse` does not accept a string. To reverse the characters of a string, use the [`reverse` function](duckdb-functions-string-general.md#athena-duckdb-functions-string-reverse).

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-list-array-reverse-differences-from-engine-version-3"></a>

In Athena engine version 3, `reverse` also reverses an array, as in `reverse(ARRAY[1, 2, 3])`. In DuckDB, `reverse` accepts only strings. To reverse a list, use `array_reverse` or `list_reverse`.

## `list_grade_up` function
<a name="athena-duckdb-functions-list-list-grade-up"></a>

Returns a list of positions in `list`, arranged so that reading the elements at those positions gives the elements in sorted order. Positions start at `1`. The first number in the result is the position of the smallest element, or of the largest element with `'DESC'`. The second number is the position of the next element, and so on. The sort order is the one that `list_sort` uses, but the function returns positions instead of values. Elements that are equal keep their original order.

By default, the sort is ascending and `NULL` elements sort last. If `list`, `sort_order`, or `null_order` is `NULL`, the result is `NULL`.

`array_grade_up` and `grade_up` are aliases of `list_grade_up`.

### Syntax
<a name="athena-duckdb-functions-list-list-grade-up-syntax"></a>

```sql
list_grade_up({{list}} [, {{sort_order}} [, {{null_order}} ] ])
```

### Arguments
<a name="athena-duckdb-functions-list-list-grade-up-arguments"></a>

The `list_grade_up` function takes the following arguments:

**{{list}}**  
The list whose sort order you want, with elements of any type that can be sorted.

**{{sort\_order}}**  
The sort direction, as a `VARCHAR` value: `'ASC'` for ascending order or `'DESC'` for descending order, in any letter case. The default is `'ASC'`. This argument is optional.

**{{null\_order}}**  
The sort position of `NULL` elements, as a `VARCHAR` value: `'NULLS FIRST'` or `'NULLS LAST'`, in any letter case. The default is `'NULLS LAST'`. This argument is optional.

### Return type
<a name="athena-duckdb-functions-list-list-grade-up-return-type"></a>

`BIGINT[]`

### Examples
<a name="athena-duckdb-functions-list-list-grade-up-examples"></a>

The following SQL query returns the positions that sort a list in ascending order. The smallest value, `1`, is at position 3, the next value, `2`, is at position 4, and so on.

```sql
SELECT list_grade_up([3, 6, 1, 2]) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[3, 4, 1, 2]` | 

The following SQL query returns the positions for descending order.

```sql
SELECT list_grade_up([3, 6, 1, 2], 'DESC') AS result
```

The query returns the following result.


| result | 
| --- | 
| `[2, 1, 4, 3]` | 

The following SQL query shows that the position of the `NULL` element, `2`, comes last in the result by default and first with `'NULLS FIRST'`.

```sql
SELECT
    list_grade_up([3, NULL, 1]) AS nulls_last,
    list_grade_up(
        [3, NULL, 1], 'ASC', 'NULLS FIRST'
    ) AS nulls_first
```

The query returns the following result.


| nulls\_last | nulls\_first | 
| --- | --- | 
| `[3, 1, 2]` | `[2, 3, 1]` | 

The following SQL query sorts a list of names by a second list of amounts. `list_select` returns the names at the positions that `list_grade_up` returns for the amounts.

```sql
SELECT
    list_select(
        ['carol', 'alice', 'bob'],
        list_grade_up([42, 120, 75])
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[carol, bob, alice]` | 

### Considerations
<a name="athena-duckdb-functions-list-list-grade-up-considerations"></a>

The result lists positions, not the rank of each element. For example, `list_grade_up([30, 10, 20])` returns `[2, 3, 1]`, because the smallest value is at position 2. To get the rank of each element instead, apply the function to its own result: `list_grade_up(list_grade_up([30, 10, 20]))` returns `[3, 1, 2]`.

Use `'ASC'` or `'DESC'` for `sort_order`, and `'NULLS FIRST'` or `'NULLS LAST'` for `null_order`. A value that the function does not recognize, such as `'UP'`, makes the query fail.

## `list_reverse` function
<a name="athena-duckdb-functions-list-list-reverse"></a>

Returns the elements of `list` in reverse order, so the last element comes first. `NULL` elements are kept, in their reversed positions. If `list` is `NULL`, the result is `NULL`.

`list_reverse` accepts only lists. To reverse the characters of a string, use the [`reverse` function](duckdb-functions-string-general.md#athena-duckdb-functions-string-reverse).

### Syntax
<a name="athena-duckdb-functions-list-list-reverse-syntax"></a>

```sql
list_reverse({{list}})
```

### Arguments
<a name="athena-duckdb-functions-list-list-reverse-arguments"></a>

The `list_reverse` function takes the following argument:

**{{list}}**  
The list to reverse, of any element type.

### Return type
<a name="athena-duckdb-functions-list-list-reverse-return-type"></a>

A list of the same type as `list`, for example `INTEGER[]`.

### Examples
<a name="athena-duckdb-functions-list-list-reverse-examples"></a>

The following SQL query reverses a list.

```sql
SELECT list_reverse([4, 5, 6]) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[6, 5, 4]` | 

The following SQL query reverses a list that has a `NULL` element. The `NULL` element stays in the result.

```sql
SELECT list_reverse(['a', NULL, 'c']) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[c, NULL, a]` | 

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-list-list-reverse-differences-from-engine-version-3"></a>

In Athena engine version 3, `reverse` also accepts an array and reverses its elements. In DuckDB, `reverse` accepts only strings, so a call such as `reverse([1, 2, 3])` fails. Use `list_reverse` to reverse a list.

## `list_reverse_sort` function
<a name="athena-duckdb-functions-list-list-reverse-sort"></a>

Returns the elements of `list` sorted from largest to smallest. By default, `NULL` elements go at the end of the result. To put them at the start, pass `'NULLS FIRST'` as `null_order`.

`list_reverse_sort({{list}})` returns the same result as `list_sort({{list}}, 'DESC')`. If `list` is `NULL`, the result is `NULL`. `array_reverse_sort` is an alias of `list_reverse_sort`.

### Syntax
<a name="athena-duckdb-functions-list-list-reverse-sort-syntax"></a>

```sql
list_reverse_sort({{list}} [, {{null_order}} ])
```

### Arguments
<a name="athena-duckdb-functions-list-list-reverse-sort-arguments"></a>

The `list_reverse_sort` function takes the following arguments:

**{{list}}**  
The list to sort. Its elements can be of any type that can be ordered.

**{{null\_order}}**  
The sort position of `NULL` elements: `'NULLS FIRST'` or `'NULLS LAST'`. The default is `'NULLS LAST'`. The value is not case-sensitive. This argument is optional.

### Return type
<a name="athena-duckdb-functions-list-list-reverse-sort-return-type"></a>

A list of the same type as `list`, for example `INTEGER[]`.

### Examples
<a name="athena-duckdb-functions-list-list-reverse-sort-examples"></a>

The following SQL query sorts a list in descending order.

```sql
SELECT list_reverse_sort([3, 6, 1, 2]) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[6, 3, 2, 1]` | 

The following SQL query sorts a list that has a `NULL` element. By default, the `NULL` element goes last.

```sql
SELECT list_reverse_sort([3, NULL, 1, 2]) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[3, 2, 1, NULL]` | 

The following SQL query puts the `NULL` element first.

```sql
SELECT
    list_reverse_sort(
        [3, NULL, 1, 2], 'NULLS FIRST'
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[NULL, 3, 2, 1]` | 

The following SQL query uses the `array_reverse_sort` alias on strings. The lowercase letters come before the uppercase `B`.

```sql
SELECT array_reverse_sort(['b', 'a', 'B']) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[b, a, B]` | 

### Considerations
<a name="athena-duckdb-functions-list-list-reverse-sort-considerations"></a>

The second argument sets only where `NULL` elements go. The order is always descending, and a sort order such as `'DESC'` in the second argument makes the query fail.

Strings are compared by their character codes, so in descending order lowercase letters come before uppercase letters.

## `list_sort` function
<a name="athena-duckdb-functions-list-list-sort"></a>

Returns the elements of `list` in sorted order. By default, the order is ascending, and `NULL` elements go at the end of the result. To change the order, pass `sort_order`. To change where `NULL` elements go, also pass `null_order`.

If `list` is `NULL`, the result is `NULL`. `array_sort` is an alias of `list_sort`. To sort in descending order, you can also use the [`list_reverse_sort` function](#athena-duckdb-functions-list-list-reverse-sort).

### Syntax
<a name="athena-duckdb-functions-list-list-sort-syntax"></a>

```sql
list_sort({{list}} [, {{sort_order}} [, {{null_order}} ] ])
```

### Arguments
<a name="athena-duckdb-functions-list-list-sort-arguments"></a>

The `list_sort` function takes the following arguments:

**{{list}}**  
The list to sort. Its elements can be of any type that can be ordered, including lists and structs.

**{{sort\_order}}**  
The sort direction: `'ASC'` for ascending order or `'DESC'` for descending order. The default is `'ASC'`. The value is not case-sensitive. This argument is optional.

**{{null\_order}}**  
The sort position of `NULL` elements: `'NULLS FIRST'` or `'NULLS LAST'`. The default is `'NULLS LAST'`. The value is not case-sensitive. This argument is optional.

### Return type
<a name="athena-duckdb-functions-list-list-sort-return-type"></a>

A list of the same type as `list`, for example `INTEGER[]`.

### Examples
<a name="athena-duckdb-functions-list-list-sort-examples"></a>

The following SQL query sorts a list in ascending order.

```sql
SELECT list_sort([3, 6, 1, 2]) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[1, 2, 3, 6]` | 

The following SQL query sorts a list in descending order.

```sql
SELECT list_sort([3, 6, 1, 2], 'DESC') AS result
```

The query returns the following result.


| result | 
| --- | 
| `[6, 3, 2, 1]` | 

The following SQL query uses the `array_sort` alias on a list that has a `NULL` element. By default, the `NULL` element goes last.

```sql
SELECT array_sort([3, NULL, 1, 2]) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[1, 2, 3, NULL]` | 

The following SQL query sorts in descending order and puts the `NULL` element first.

```sql
SELECT
    list_sort(
        [3, NULL, 1, 2], 'DESC', 'NULLS FIRST'
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[NULL, 3, 2, 1]` | 

### Considerations
<a name="athena-duckdb-functions-list-list-sort-considerations"></a>

To pass `null_order`, also pass `sort_order`, as in `list_sort({{list}}, 'ASC', 'NULLS FIRST')`. A call such as `list_sort({{list}}, 'NULLS FIRST')` fails, because the second argument is always the sort order.

`list_sort` does not accept a comparison lambda. To sort a list by a computed key or by the values of another list, use the [`list_select` function](duckdb-functions-list-length-and-elements.md#athena-duckdb-functions-list-list-select) with `list_grade_up`.

Strings are compared by their character codes, so in ascending order uppercase letters come before lowercase letters.

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-list-list-sort-differences-from-engine-version-3"></a>

In Athena engine version 3, `array_sort` accepts a comparison function as its second argument, such as `array_sort(ARRAY[3, 1, 2], (x, y) -> IF(x < y, 1, IF(x = y, 0, -1)))`. In DuckDB, `array_sort` is an alias of `list_sort`, which does not accept a function. To sort in descending order, pass `'DESC'` or use the [`list_reverse_sort` function](#athena-duckdb-functions-list-list-reverse-sort).