If no lambda function is specified, the array elements are sorted in ascending order. Otherwise, sorting is performed according to the lambda function.
ARRAY_SORT(<arr>) ARRAY_SORT(<lambda>, <arr>)
| Parameter | Description |
|---|---|
<arr> | ARRAY whose elements can be numeric, boolean, string, datetime, IP, UUID, or other supported types. Multi-dimensional arrays are supported, but all nested elements must be of these supported types. |
<lambda> | Optional comparator that accepts two elements and returns -1, 0, or 1 when the first element is less than, equal to, or greater than the second. |
lambda expressionARRAY<T> of the same type as the input.NULL elements are placed at the beginning of the returned array.lambda expression, sort according to the lambda expression and return an ARRAY<T> of the same type as the input.In the Doris 4.1 series since 4.1.5, a comparator that references an outer lambda argument or another column explicitly reports an error. The comparator may use only its own two arguments, and a lambda nested inside it cannot capture those comparator arguments. This restriction does not affect ordinary nested lambda capture in ARRAY_MAP.
lambda expressionNULL, returns NULL; if the input is an empty array [], returns an empty array.ARRAY_SORT sorts in ascending order, while ARRAY_REVERSE_SORT sorts in descending order.lambda expressionlambda expression shall be -1, 0, or 1. Should a NULL value exist, it shall be explicitly stated whether the NULL value is to be placed before or after the array.NULL elements are placed at the beginning of the returned arraySELECT ARRAY_SORT([2,1,3,null]);
+--------------------------+ | ARRAY_SORT([2,1,3,null]) | +--------------------------+ | [null, 1, 2, 3] | +--------------------------+
NULL, returns NULL; if the input is an empty array [], returns an empty array.SELECT ARRAY_SORT(NULL);
+------------------+ | ARRAY_SORT(NULL) | +------------------+ | NULL | +------------------+
SELECT ARRAY_SORT([]);
+----------------+ | ARRAY_SORT([]) | +----------------+ | [] | +----------------+
lambda expressionsSELECT array_sort((x, y) -> IF(x < y, 1, IF(x = y, 0, -1)), [3, 2, 5, 1, 2]);
+-----------------------------------------------------------------------+ | array_sort((x, y) -> IF(x < y, 1, IF(x = y, 0, -1)), [3, 2, 5, 1, 2]) | +-----------------------------------------------------------------------+ | [5, 3, 2, 2, 1] | +-----------------------------------------------------------------------+
SELECT array_sort((x, y) -> IF(x < y, 1, IF(x = y, 0, -1)), ['bc', 'ab', 'dc']);
+--------------------------------------------------------------------------+ | array_sort((x, y) -> IF(x < y, 1, IF(x = y, 0, -1)), ['bc', 'ab', 'dc']) | +--------------------------------------------------------------------------+ | ["dc", "bc", "ab"] | +--------------------------------------------------------------------------+
SELECT array_sort((x, y) -> CASE WHEN x IS NULL THEN -1 WHEN y IS NULL THEN 1 WHEN x < y THEN 1 WHEN x = y THEN 0 ELSE -1 END, [3, 2, null, 5, null, 1, 2]);
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | array_sort((x, y) -> CASE WHEN x IS NULL THEN -1 WHEN y IS NULL THEN 1 WHEN x < y THEN 1 WHEN x = y THEN 0 ELSE -1 END, [3, 2, null, 5, null, 1, 2]) | +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | [null, null, 5, 3, 2, 2, 1] | +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
SELECT array_sort((x, y) -> CASE WHEN x IS NULL THEN 1 WHEN y IS NULL THEN -1 WHEN x < y THEN 1 WHEN x = y THEN 0 ELSE -1 END, [3, 2, null, 5, null, 1, 2]);
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | array_sort((x, y) -> CASE WHEN x IS NULL THEN 1 WHEN y IS NULL THEN -1 WHEN x < y THEN 1 WHEN x = y THEN 0 ELSE -1 END, [3, 2, null, 5, null, 1, 2]) | +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | [5, 3, 2, 2, 1, null, null] | +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
SELECT array_sort((x, y) -> IF(length(x) < length(y), -1, IF(length(x) = length(y), 0, 1)), ['a', 'abcd', 'abc']);
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | array_sort((x, y) -> IF(length(x) < length(y), -1, IF(length(x) = length(y), 0, 1)), ['a', 'abcd', 'abc']) | +--------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | ["a", "abc", "abcd"] | +--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
SELECT array_sort((x, y) -> IF(cardinality(x) < cardinality(y), -1, IF(cardinality(x) = cardinality(y), 0, 1)), [[2, 3, 1], [4, 2, 1, 4], [1, 2]]);
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | array_sort((x, y) -> IF(cardinality(x) < cardinality(y), -1, IF(cardinality(x) = cardinality(y), 0, 1)), [[2, 3, 1], [4, 2, 1, 4], [1, 2]]) | +-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | [[1, 2], [2, 3, 1], [4, 2, 1, 4]] | +-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
SELECT ARRAY_SORT([[6, 2], [5, 6]]);
+------------------------------+ | ARRAY_SORT([[6, 2], [5, 6]]) | +------------------------------+ | [[5, 6], [6, 2]] | +------------------------------+