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

# SORT_ARRAY

> Sorts array elements.

<div className="e6-meta"><span className="e6-eyebrow e6-eyebrow--quiet">Scalar function</span></div>

NULL array or NULL flag returns NULL.

## Syntax

### Scalar

```sql theme={"theme":{"light":"github-light","dark":"github-dark"}}
sort_array(array [, ascending])
```

## Arguments

Type notation used below:

* T is the array element type: BOOLEAN, TINYINT, SMALLINT, INTEGER, BIGINT, FLOAT, DOUBLE, DECIMAL(p, s), STRING, BINARY, DATE, TIME, TIMESTAMP, ARRAY or STRUCT.

| Name | Type | Required | Description |
| - | - | - | - |
| array | ARRAY\<T> | Yes | The array. Nested elements/fields must satisfy the same element-type restrictions. |
| ascending | BOOLEAN | No | TRUE ascending, FALSE descending. Must be a constant argument. Default: TRUE. |

## Returns

* Type: ARRAY\<T>
* Possible values: Sorted elements.
* NULL behavior: NULL array or NULL flag returns NULL.
* Shape: Same element count/type.
* Ordering: Ascending NULLs first, or descending NULLs last.

## Properties

| Property | Value |
| - | - |
| Deterministic | Yes |
| Supports window usage | No |
| Exact result | Yes |

## Usage notes

* Floating-point sorting distinguishes positive and negative zero and different NaN values. STRING and BINARY values use lexicographic order. Nested arrays and structs are sorted by their elements or fields.
* INTEGER and INT mean the same type; STRING means VARCHAR text. NULL can be used with a supported type. Cast NULL explicitly when its type would otherwise be unclear.

## Examples

### Typical behavior

```sql theme={"theme":{"light":"github-light","dark":"github-dark"}}
SELECT sort_array(ARRAY[3, NULL, 1], TRUE) AS ascending, sort_array(ARRAY[3, NULL, 1], FALSE) AS descending;
```

| ascending | descending |
| - | - |
| \[null,1,3] | \[3,1,null] |

### Sort strings with a NULL element

```sql theme={"theme":{"light":"github-light","dark":"github-dark"}}
SELECT sort_array(ARRAY('pear', CAST(NULL AS VARCHAR), 'apple')) AS sorted_values;
```

| sorted\_values |
| - |
| \[null,"apple","pear"] |

## Limitations

* ascending must be BOOLEAN literal; elements must support sorting.
* Elements must have comparable types. The relative order of equal values is not guaranteed.
* MAP elements, including MAP values nested inside compared elements, are outside the supported element types.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.