SORT_ARRAY

Overview

Sorts the elements of an array and returns a new sorted array. NULL values are always placed at the beginning.

Syntax

SORT_ARRAY(<array> [, <asc>])

Parameters

  • <array>: ARRAY<T> type, the array to sort.
  • <asc>: BOOLEAN type, optional, defaults to true (ascending order); pass false for descending order. NULL values are placed at the beginning regardless of sort direction.

Examples

SELECT sort_array(array(2, 1, 3)); -- [1,2,3] SELECT sort_array(array(null, 4, 3, null, 5, 6)); -- [null,null,3,4,5,6] SELECT sort_array(array(2, 1, 3), false); -- [3,2,1]

  • ARRAY_SORT — alias with identical functionality
  • REVERSE — reverses the order of array elements