> ## 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.

# ARRAY_SORT

> Sorts an array naturally or with a comparator lambda.

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

NULL array returns NULL. Natural ordering retains NULL elements last; comparator sees NULL elements and must supply their ordering.

## Syntax

### Scalar

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

## 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 | Input elements. Nested elements/fields must satisfy the same element-type restrictions. |
| comparator | FUNCTION(T, T) -> INTEGER | No | Returns negative, zero or positive according to ordering. Nested elements/fields must satisfy the same element-type restrictions. Default: Natural ascending order. |

## Returns

* Type: ARRAY\<T>
* Possible values: The sorted input elements.
* NULL behavior: NULL array returns NULL. Natural ordering retains NULL elements last; comparator sees NULL elements and must supply their ordering.
* Shape: Same element count and type as input.
* Ordering: Natural ascending with NULLs last, or comparator-defined order.

## Properties

| Property | Value |
| - | - |
| Deterministic | Partial (Natural sorting is deterministic; comparator sorting requires a deterministic, consistent comparator.) |
| Supports window usage | No |

## 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.
* Comparator sorting preserves encounter order of values whose comparison is equal. The comparator is evaluated on pairs including NULL elements, must return a non-NULL signed integer, and propagates evaluation errors.
* 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 array_sort(ARRAY[3, NULL, 1]) AS "value";
```

| value |
| - |
| \[1,3,null] |

### Sort strings with a NULL element

Natural ARRAY\_SORT ordering places NULL elements last.

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

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

## Limitations

* Comparator must return an integer and must not return NULL.
* Elements must have comparable types. With natural sorting, the relative order of equal values is not guaranteed. With a comparator, values that compare as equal keep their original relative order.
* 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.