Sorts the elements in an array.
Syntax
array<T> sort_array(array<T> <a>[, <isasc>])
Parameters
a: required. This parameter specifies an array.
T
inarray<T>
specifies the data type of the elements in the array. The elements can be of any data type.isasc: optional. This parameter specifies the order in which elements in the array are sorted. Valid values: true and false. The value true indicates the ascending order, and the value false indicates the descending order. If you do not specify this parameter, the elements are sorted in ascending order.
Return value
A value of the ARRAY type is returned. The value null is interpreted as the minimum value.
Examples
Example 1: Sort data in the
t_array
table that contains thec1 ARRAY<STRING>, c2 ARRAY<INT>, and c3 ARRAY<STRING>
columns. Data in the table:+------------+---------+--------------+ | c1 | c2 | c3 | +------------+---------+--------------+ | [a, c, f, b] | [4, 5, 7, 2, 5, 8] | [You, Me, Him] | +------------+---------+--------------+
Sort data in each column of the table. Sample statement:
-- The return value is [a, b, c, f] [2, 4, 5, 5, 7, 8] [Him, Me, You]. select sort_array(c1),sort_array(c2),sort_array(c3) from t_array;
Example 2: Sort the elements in
array(10, 20, 40, 30, 30, null, 50)
in descending order. Sample statement:-- The return value is [50, 40, 30, 30, 20, 10, null]. select sort_array(array(10, 20, 40, 30, 30, null, 50), false);
Related functions
SORT_ARRAY is a complex type function. For more information about the functions that are used to process data of complex data types, such as ARRAY, MAP, STRUCT, and JSON, see Complex type functions.