Skip to content

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, function and method notation. These interchangeable forms allow flexibility in how you structure expressions.

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

Arguments

NameTypeRequiredDescription
arrayarraytrueThe array to sort
descboolfalseSort order flag. When true, the array is sorted in descending order. Must be a literal. Defaults to false.
nullsFirstboolfalseNull 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.

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

Output

The result will include the array sorted alphabetically:

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