Sort List | ARRAY_SORT

Syntax

ARRAY_SORT( [value], [direction = 1] )

Parameters

value
An ARRAY to sort.

direction optional
Set whether to sort in ascending or descending order. If the value is a NUMBER that is greater than or equal to 0, sorting will be done in ascending order. If the value is a NUMBER that is less than 0, sorting will done in descending order. If the value is not a NUMBER, sorting will be done in ascending order.

Return Value

Returns a sorted copy of the VALUE.

Description

Sort the items in an ARRAY. Comparison for the sort is done structurally. If values of different types are provided, the comparison is done by type with the following precedence (from lowest to highest):
  1. UNDEFINED
  2. NULL
  3. BOOLEAN
  4. NUMBER
  5. STRING
  6. DATE
  7. ARRAY / LIST
  8. OBJECT / MAP
UNDEFINED values are always considered equal. NULL values are also always considered equal. FALSE is less than TRUE. NUMBER values are compared as one would expect. STRING values are compared alphanumerically. DATE values are compared normally. ARRAY values are compared item by item where the first non-equal value is the result. MAP value are currently always considered equal, but this is likely to change in a future update, and should not be relied upon.

Examples

ARRAY_SORT(null) # returns []
# VALUE is [
#   '1', '2', 1, 1, 2, '3', '3'
# ]
ARRAY_SORT(VALUE)
# returns [
#   1, 1, 2, '1', '2', '3', '3'
# ]
ARRAY values are sorted structurally.
# VALUE is [
#   [1, 2, 3],
#   [1, 3, 2],
#   [1, 2, 4],
#   [1, 2, 3, 0]
# ]
ARRAY_SORT(VALUE)
# returns [
#   [1, 2, 3],
#   [1, 2, 3, 0],
#   [1, 2, 4]
#   [1, 3, 4]
# ]