---
title: array_sort function
description: Learn the syntax of the array\_sort function of the SQL language in Databricks SQL and Databricks Runtime.
canonical_url: https://docs.databricks.com/aws/en/sql/language-manual/functions/array_sort
---

# `array_sort` function

**Applies to:** ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks SQL ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks Runtime

Returns `array` sorted according to `func`.

## Syntax

```
array_sort(array, func)
```

## Arguments

* `array`: An expression that evaluates to an array.
* `func`: A lambda function defining the sort order.

## Returns

The result type matches the type of `array`.

If `func` is omitted, the array is sorted in ascending order.

If `func` is provided it takes two arguments representing two elements of the array.

The function must return -1, 0, or 1 depending on whether the first element is less than, equal to, or greater than the second element.

Databricks raises [COMPARATOR\_RETURNS\_NULL](https://docs.databricks.com/aws/en/error-messages/error-classes.md#comparator_returns_null) if the `func` returns NULL.

NULL elements are placed at the end of the returned array.

## Common error conditions

* [COMPARATOR\_RETURNS\_NULL](https://docs.databricks.com/aws/en/error-messages/error-classes.md#comparator_returns_null)

## Examples

```sql
> SELECT array_sort(array(5, 6, 1),
                   (left, right) -> CASE WHEN left < right THEN -1
                                         WHEN left > right THEN 1 ELSE 0 END);
 [1,5,6]
> SELECT array_sort(array('bc', 'ab', 'dc'),
                    (left, right) -> CASE WHEN left IS NULL and right IS NULL THEN 0
                                          WHEN left IS NULL THEN -1
                                          WHEN right IS NULL THEN 1
                                          WHEN left < right THEN 1
                                          WHEN left > right THEN -1 ELSE 0 END);
 [dc,bc,ab]
> SELECT array_sort(array('b', 'd', null, 'c', 'a'));
 [a,b,c,d,NULL]

-- Comparator must return -1, 0, or 1, not NULL
> SELECT array_sort(array(1, 2), (l, r) -> NULL);
  Error: COMPARATOR_RETURNS_NULL
```

## Related functions

* [`array_append` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/array_append.md)
* [`array_compact` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/array_compact.md)
* [`array_distinct` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/array_distinct.md)
* [`array_except` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/array_except.md)
* [`array_intersect` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/array_intersect.md)
* [`array_remove` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/array_remove.md)
* [`array_union` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/array_union.md)
* [`sort_array` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/sort_array.md)
* [`zip_with` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/zip_with.md)
