---
schema_version: "1"
id: 0e77715bd5da5dd0c8838b8a90ebf24c992094f2bf296d9ce9a9e3ae7dd18ec8
title: ARRAY_SORT
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/data-cloud/data-cloud-query-guide/references/data-cloud-sql-reference/scalar-func/array-func/array-sort.md
canonical_url: https://developer.salesforce.com/docs/data/data-cloud-query-guide/references/dc-sql-reference/array-sort.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 2d644835132f971fb164d9eed8349c97ed6ed1a5d0690a92f5d08205518cca97
products:
  - Data 360 Query Guide
  - Data 360 Developer Documentation
---
# `ARRAY_SORT`

**Applies to:** ✅ Data 360 SQL ✅ Tableau Hyper API

Sorts the elements of an array in ascending or descending order.

## Syntax

```sql
array_sort(<input_array>[, ascending])
```

## Arguments

### Required

- `<input_array>`: The array to sort.

### Optional

- `ascending`: A boolean flag that indicates the sort order. When `true` (default), sorts in ascending order. When `false`, sorts in descending order.

## Returns

Returns an array with elements sorted in the specified order. NULL values are always first in the output array.

## Considerations

- NULL values are always first in the output array, regardless of sort order.
- The default sort order is ascending.

## Examples

### Sort in Ascending Order

Sort an array in ascending order (default behavior).

```sql
SELECT array_sort(array[3, 1, 4, 1, 5, 9, 2, 6]) AS result;
```

Returns `array[1, 1, 2, 3, 4, 5, 6, 9]`, sorted in ascending order.

### Sort in Descending Order

Sort an array in descending order using the optional flag.

```sql
SELECT array_sort(array[3, 1, 4, 1, 5, 9, 2, 6], false) AS result;
```

Returns `array[9, 6, 5, 4, 3, 2, 1, 1]`, sorted in descending order.

### Sort with NULL Values

Sort an array that contains NULL values.

```sql
SELECT array_sort(array[3, null, 1, null, 2]) AS result;
```

Returns `array[null, null, 1, 2, 3]`. NULL values are first, followed by the sorted non-NULL values.

## Related Documentation

- [Array Data Type](https://developer.salesforce.com/docs/data/data-cloud-query-guide/datatype/array.md)
