SORT

SORT sorts a collection by the values returned from applying a sorting function to each element in the collection.

Function category: Collection

Syntax

SORT(arg1, arg2)

Argument

Description

arg1

A key function.

arg2

A collection to be sorted.

Examples

Let's say we're given a response with the following vehicle information:

{
"data":{
"fleet_prices": [16000, 17450, 9200],
"fleet_names": ["BMW", "Audi", "Mercedes Benz"]
}
}

Example 1: Sort prices by numerical value

If we want to organize these prices by basic numerical value, we can use SORT with the dynamic references themselves serving as a sorting function.

# Sort prices using dynamic references
SORT(data.fleet_prices, _)
# Returns [9200, 16000, 17450]

Example 2: Sort strings using same arguments

We can also sort strings using SORT with dynamic references.

# Sort strings using dynamic references
SORT(data.fleet_names, _)
# Returns ["Audi", "BMW", "Mercedes Benz"]

Example 3: Sort using a dynamic reference

We can also apply a function such as LEN to the dynamic reference. This sorts by length of string.

# Sort strings using the LEN function with dynamic references
SORT(data.fleet_names, LEN(_))
# Returns ["Mercedes Benz", "Audi", "BMW",]