Copy as Markdown[Open in ChatGPT](https://chatgpt.com/?q=Read%20https%3A%2F%2Fdocs-docusaurus.kinsta.page%2Fdataprime%2Flanguage-reference%2Ffunctions-reference%2Farray%2Farraysort.md%20and%20help%20me%20with%20my%20question%20about%20this%20Coralogix%20documentation%20page.)[Open in Claude](https://claude.ai/new?q=Read%20https%3A%2F%2Fdocs-docusaurus.kinsta.page%2Fdataprime%2Flanguage-reference%2Ffunctions-reference%2Farray%2Farraysort.md%20and%20help%20me%20with%20my%20question%20about%20this%20Coralogix%20documentation%20page.)

# `arraySort`

## Description

Returns a new array with the elements sorted according to the specified options.

* The element type must match the array type.
* Supported element types include `string`, `bool`, `number`, `interval`, `timestamp`, `regexp`, and `enum`.
* By default, the array is sorted in ascending order, with null values appearing last.

## Syntax

Like many functions in DataPrime, `arraySort` supports<!-- --> [two notations](https://docs-docusaurus.kinsta.page/dataprime/language-reference/functions-reference/.md),<!-- --> **function** and **method**. These interchangeable forms allow flexibility in how you structure expressions.

* Function notation
* Method notation

```
arraySort(array: array<T>, desc: bool?, nullsFirst: bool?): array<T>
```

```
(array: array<T>).arraySort(desc: bool?, nullsFirst: bool?): array<T>
```

## Arguments

| Name         | Type       | Required  | Description                                                                                                              |
| ------------ | ---------- | --------- | ------------------------------------------------------------------------------------------------------------------------ |
| `array`      | `array<T>` | **true**  | The array to sort                                                                                                        |
| `desc`       | `bool`     | **false** | Sort order flag. When `true`, the array is sorted in descending order. Must be a literal. Defaults to `false`.           |
| `nullsFirst` | `bool`     | **false** | Null placement flag. When `true`, null values appear at the start of the output. Must be a literal. Defaults to `false`. |

## Example

**Use case: Sort names alphabetically**

Suppose you have a list of names. Consider the following input:

```
{

    "names": ["Chris", "John", "Adam", "Jose"]

}
```

By applying `arraySort`, you can sort the list alphabetically in ascending order.

### Example query

* Function notation
* Method notation

```
replace names with arraySort(names)
```

```
replace names with names.arraySort()
```

### Example output

The result will include the array sorted alphabetically:

```
{

    "names": ["Adam", "Chris", "John", "Jose"]

}
```
