ARRAY_SORT()

All functions > ARRAY > ARRAY_SORT()

Returns the sorted version of an array.

Signatures

Returns: A new array with all elements sorted in ascending order

ARRAY_SORT(array: ARRAY<T>) → ARRAY<T>
sql
ParameterTypeRequiredDescription
arrayARRAY<T>YesThe input array to sort

Notes

  • Sorts elements in ascending natural order
  • Numbers: numerical order (1, 2, 3...)
  • Strings: lexicographical order (A, B, C...)
  • Dates: chronological order (earliest to latest)
  • NULL values are placed at the end of the sorted array
  • Preserves duplicate values in the sorted output
  • Supports ARRAY<ROW> and ARRAY<ARRAY<T>>: rows and sub-arrays are sorted lexicographically by field values

See also

Examples

FeatureQL
SELECT
    f1 := ARRAY_SORT(ARRAY[3, 1, 4, 1, 5]), -- Sort numbers in ascending order
    f2 := ARRAY_SORT(ARRAY['zebra', 'apple', 'banana']), -- Sort strings lexicographically
    f3 := ARRAY_SORT(ARRAY['Z', 'a', 'B']), -- Case-sensitive sorting
    f4 := ARRAY_SORT(ARRAY[10, 2, 30, 4]), -- Numerical not string sorting
    f5 := ARRAY_SORT(ARRAY[5]), -- Single element array
    f6 := ARRAY_SORT(ARRAY[]::BIGINT[]), -- Empty array
    f7 := ARRAY_SORT(ARRAY[2, NULL::BIGINT, 1, NULL::BIGINT, 3]) -- NULL values at end
;
Result
f1 ARRAYf2 ARRAYf3 ARRAYf4 ARRAYf5 ARRAYf6 ARRAYf7 ARRAY
[1, 1, 3, 4, 5][apple, banana, zebra][B, Z, a][2, 4, 10, 30][5][][1, 2, 3, null, null]

Last update at: 2026/05/26 17:22:09