> ## Documentation Index
> Fetch the complete documentation index at: https://docs.firebolt.io/llms.txt
> Use this file to discover all available pages before exploring further.

> Reference material for ARRAY_SORT function

# ARRAY_SORT

Returns the elements of the input array in ascending order.

If the argument `<function>` is provided, the sort order is determined by the result of applying `<function>` on each element of the array.

## Syntax

`ARRAY_SORT` supports two signatures:

```sql theme={"theme":{"light":"css-variables","dark":"css-variables"}}
-- One array argument:
ARRAY_SORT(<array>)

-- One function and one or multiple array arguments:
ARRAY_SORT(<function>, <array> [, ...])
```

## Parameters

| Parameter    | Description                                            | Supported input type                                              |
| :----------- | :----------------------------------------------------- | :---------------------------------------------------------------- |
| `<function>` | An optional function used to determine the sort order. | Any lambda function that takes the elements of `<array>` as input |
| `<array>`    | The array to be sorted.                                | Any array                                                         |

## Return Type

`ARRAY` of the same type as the input array

## Examples

<div className="query-window">
  ```
  SELECT ARRAY_SORT([4, 1, 3, 2]);
  ```

  | array\_sort <span>array(int)</span> |
  | :---------------------------------- |
  | \[1, 2, 3, 4]                       |

  <p><span>Rows: 1</span><span>Execution time: 5.52ms</span></p>
</div>

In this example, the modulus operator is used to calculate the remainder on any odd numbers. Therefore `ARRAY_SORT` puts the higher (odd) numbers last in the results.

<div className="query-window">
  ```
  SELECT ARRAY_SORT(x -> x % 2, [4, 1, 3, 2]);
  ```

  | array\_sort <span>array(int)</span> |
  | :---------------------------------- |
  | \[4, 2, 1, 3]                       |

  <p><span>Rows: 1</span><span>Execution time: 5.54ms</span></p>
</div>

In this example, two array arguments are passed in. Therefore, a lambda with two parameters must be used as the first argument.

<div className="query-window">
  ```
  SELECT ARRAY_SORT(x, y -> y, [1, 2, 3], ['c', 'b', 'a']);
  ```

  | array\_sort <span>array(int)</span> |
  | :---------------------------------- |
  | \[3, 2, 1]                          |

  <p><span>Rows: 1</span><span>Execution time: 5.28ms</span></p>
</div>
