ORDER

Sort an array

Description

Sorts an array using a formula that is evaluated for each item in the array. Returns the sorted array.

The formula should result to a comparable value (a string or a number) that is used to determine the order of the items. By default the items are sorted in ascending order. By providing the 3rd parameter you can choose the direction.

If multiple values have the same ordering value, their original order will be preserved.

Note that for string values, the ordering is case-sensitive. If you want a case-insensitive ordering, you may for example wrap your ordering formula with LOWERCASE function.

Function signature

ORDER(array: Array, sortingcriteria: Function, sortdirection: String = asc) => Array

Parameter

Description

Array

Array of items to sort

Sorting criteria

Formula evaluated for each item in the array, used to determine the ordering value for the item

Sort direction

Direction of the ordering. Use either "asc" meaning ascending order (smallest first) or "desc" meaning descending order (greatest first).

Examples

animals = [
{
"name": "dog",
"weight": 123
},
{
"name": "cat",
"weight": 48
},
{
"name": "rat",
"weight": 10
},
{
"name": "pig",
"weight": 123
}
]

Formula

Return value

ORDER(animals, item.weight)

[{"name":"rat","weight":10},{"name":"cat","weight":48},{"name":"dog","weight":123},{"name":"pig","weight":123}]

ORDER(animals, item.weight, "desc")

[{"name":"dog","weight":123},{"name":"pig","weight":123},{"name":"cat","weight":48},{"name":"rat","weight":10}]

ORDER<animal>(animals, animal.weight, "asc")

[{"name":"rat","weight":10},{"name":"cat","weight":48},{"name":"dog","weight":123},{"name":"pig","weight":123}]