# `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://coralogix.com/docs/dataprime/language-reference/functions-reference/index.md), **function** and **method** notation. These interchangeable forms allow flexibility in how you structure expressions.

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

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

## Arguments

| Name       | Type  | Required  | Description                                                                                                              |
| ---------- | ----- | --------- | ------------------------------------------------------------------------------------------------------------------------ |
| array      | array | **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:

```json
{
    "names": ["Chris", "John", "Adam", "Jose"]
}
```

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

### Example query

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

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

### Example output

The result will include the array sorted alphabetically:

```json
{
    "names": ["Adam", "Chris", "John", "Jose"]
}
```
