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

# `sort_array` 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 the array in `expr` in sorted order.

## Syntax

```
sort_array(expr [, ascendingOrder] )
```

## Arguments

* `expr`: An `ARRAY` expression of sortable elements.
* `ascendingOrder`: An optional `BOOLEAN` expression defaulting to `true`.

## Returns

The result type matches `expr`.

Sorts the input array in ascending or descending order according to the natural ordering of the array elements.
`NULL` elements are placed at the beginning of the returned array in ascending order or at the end of the returned array in descending order.

## Examples

```sql
> SELECT sort_array(array('b', 'd', NULL, 'c', 'a'), true);
 [NULL,a,b,c,d]
```

## Related functions

* [`array_sort` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/array_sort.md)
