|
7 | 7 |
|
8 | 8 | ## Function |
9 | 9 |
|
10 | | -Sort array elements in ascending order. |
| 10 | +If no lambda function is specified, the array elements are sorted in ascending order. Otherwise, sorting is performed according to the lambda function. |
11 | 11 |
|
12 | 12 | ## Syntax |
13 | 13 |
|
14 | 14 | - `ARRAY_SORT(arr)` |
| 15 | +- `ARRAY_SORT(lambda, arr)` |
15 | 16 |
|
16 | 17 | ## Parameters |
17 | 18 |
|
| 19 | +- `lambda`: A `lambda` expression used to define sorting rules, whose return value should be -1, 0, or 1 (representing less than, less than or equal to, and greater than respectively). |
18 | 20 | - `arr`: `ARRAY<T>`, where `T` can be numeric, boolean, string, datetime, IP, etc. |
19 | 21 |
|
20 | 22 | ## Return value |
21 | 23 |
|
22 | | -- Returns `ARRAY<T>` of the same type as the input. |
23 | | -- `NULL` elements are placed at the beginning of the returned array. |
| 24 | +- If it does not contain a `lambda` expression |
| 25 | + - Returns `ARRAY<T>` of the same type as the input. |
| 26 | + - `NULL` elements are placed at the beginning of the returned array. |
| 27 | +- If it contains a `lambda` expression, sort according to the lambda expression and return an `ARRAY<T>` of the same type as the input. |
24 | 28 |
|
25 | 29 | ## Usage notes |
26 | 30 |
|
27 | | -- If the input is `NULL`, returns `NULL`; if the input is an empty array `[]`, returns an empty array. |
28 | | -- `ARRAY_SORT` sorts in ascending order, while `ARRAY_REVERSE_SORT` sorts in descending order. |
| 31 | +- If it does not contain a `lambda` expression |
| 32 | + - If the input is `NULL`, returns `NULL`; if the input is an empty array `[]`, returns an empty array. |
| 33 | + - `ARRAY_SORT` sorts in ascending order, while `ARRAY_REVERSE_SORT` sorts in descending order. |
| 34 | +- If containing a `lambda` expression |
| 35 | + - The value of the `lambda` 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. |
29 | 36 |
|
30 | 37 | ## Examples |
31 | 38 |
|
32 | | -- Basic: `NULL` elements are placed at the beginning of the returned array |
33 | | - - `ARRAY_SORT([2,1,3,null])` -> `[null, 1, 2, 3]` |
34 | | - |
35 | | -- If the input is `NULL`, returns `NULL`; if the input is an empty array `[]`, returns an empty array. |
36 | | - - `ARRAY_SORT(NULL)` -> `NULL` |
37 | | - - `ARRAY_SORT([])` -> `[]` |
38 | | - |
39 | | - |
40 | | - |
| 39 | +1. Basic: `NULL` elements are placed at the beginning of the returned array |
| 40 | + |
| 41 | +```sql |
| 42 | +SELECT ARRAY_SORT([2,1,3,null]); |
| 43 | +``` |
| 44 | + |
| 45 | +```text |
| 46 | ++--------------------------+ |
| 47 | +| ARRAY_SORT([2,1,3,null]) | |
| 48 | ++--------------------------+ |
| 49 | +| [null, 1, 2, 3] | |
| 50 | ++--------------------------+ |
| 51 | +``` |
| 52 | + |
| 53 | +2. If the input is `NULL`, returns `NULL`; if the input is an empty array `[]`, returns an empty array. |
| 54 | + |
| 55 | +```sql |
| 56 | +SELECT ARRAY_SORT(NULL); |
| 57 | +``` |
| 58 | + |
| 59 | +```text |
| 60 | ++------------------+ |
| 61 | +| ARRAY_SORT(NULL) | |
| 62 | ++------------------+ |
| 63 | +| NULL | |
| 64 | ++------------------+ |
| 65 | +``` |
| 66 | + |
| 67 | +```sql |
| 68 | +SELECT ARRAY_SORT([]); |
| 69 | +``` |
| 70 | + |
| 71 | +```text |
| 72 | ++----------------+ |
| 73 | +| ARRAY_SORT([]) | |
| 74 | ++----------------+ |
| 75 | +| [] | |
| 76 | ++----------------+ |
| 77 | +``` |
| 78 | + |
| 79 | +3. Use `lambda` expressions |
| 80 | + |
| 81 | +```sql |
| 82 | +SELECT array_sort((x, y) -> IF(x < y, 1, IF(x = y, 0, -1)), [3, 2, 5, 1, 2]); |
| 83 | +``` |
| 84 | + |
| 85 | +```text |
| 86 | ++-----------------------------------------------------------------------+ |
| 87 | +| array_sort((x, y) -> IF(x < y, 1, IF(x = y, 0, -1)), [3, 2, 5, 1, 2]) | |
| 88 | ++-----------------------------------------------------------------------+ |
| 89 | +| [5, 3, 2, 2, 1] | |
| 90 | ++-----------------------------------------------------------------------+ |
| 91 | +``` |
| 92 | + |
| 93 | +```sql |
| 94 | +SELECT array_sort((x, y) -> IF(x < y, 1, IF(x = y, 0, -1)), ['bc', 'ab', 'dc']); |
| 95 | +``` |
| 96 | + |
| 97 | +```text |
| 98 | ++--------------------------------------------------------------------------+ |
| 99 | +| array_sort((x, y) -> IF(x < y, 1, IF(x = y, 0, -1)), ['bc', 'ab', 'dc']) | |
| 100 | ++--------------------------------------------------------------------------+ |
| 101 | +| ["dc", "bc", "ab"] | |
| 102 | ++--------------------------------------------------------------------------+ |
| 103 | +``` |
| 104 | + |
| 105 | +```sql |
| 106 | +SELECT array_sort((x, y) -> CASE WHEN x IS NULL THEN -1 |
| 107 | + WHEN y IS NULL THEN 1 |
| 108 | + WHEN x < y THEN 1 |
| 109 | + WHEN x = y THEN 0 |
| 110 | + ELSE -1 END, |
| 111 | + [3, 2, null, 5, null, 1, 2]); |
| 112 | +``` |
| 113 | + |
| 114 | +```text |
| 115 | ++------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ |
| 116 | +| array_sort((x, y) -> CASE WHEN x IS NULL THEN -1 |
| 117 | + WHEN y IS NULL THEN 1 |
| 118 | + WHEN x < y THEN 1 |
| 119 | + WHEN x = y THEN 0 |
| 120 | + ELSE -1 END, |
| 121 | + [3, 2, null, 5, null, 1, 2]) | |
| 122 | ++------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ |
| 123 | +| [null, null, 5, 3, 2, 2, 1] | |
| 124 | ++------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ |
| 125 | +``` |
| 126 | + |
| 127 | +```sql |
| 128 | +SELECT array_sort((x, y) -> CASE WHEN x IS NULL THEN 1 |
| 129 | + WHEN y IS NULL THEN -1 |
| 130 | + WHEN x < y THEN 1 |
| 131 | + WHEN x = y THEN 0 |
| 132 | + ELSE -1 END, |
| 133 | + [3, 2, null, 5, null, 1, 2]); |
| 134 | +``` |
| 135 | + |
| 136 | +```text |
| 137 | ++------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ |
| 138 | +| array_sort((x, y) -> CASE WHEN x IS NULL THEN 1 |
| 139 | + WHEN y IS NULL THEN -1 |
| 140 | + WHEN x < y THEN 1 |
| 141 | + WHEN x = y THEN 0 |
| 142 | + ELSE -1 END, |
| 143 | + [3, 2, null, 5, null, 1, 2]) | |
| 144 | ++------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ |
| 145 | +| [5, 3, 2, 2, 1, null, null] | |
| 146 | ++------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ |
| 147 | +``` |
| 148 | + |
| 149 | +```sql |
| 150 | +SELECT array_sort((x, y) -> IF(length(x) < length(y), -1, |
| 151 | + IF(length(x) = length(y), 0, 1)), |
| 152 | + ['a', 'abcd', 'abc']); |
| 153 | +``` |
| 154 | + |
| 155 | +```text |
| 156 | ++--------------------------------------------------------------------------------------------------------------------------------------------------------------------+ |
| 157 | +| array_sort((x, y) -> IF(length(x) < length(y), -1, |
| 158 | + IF(length(x) = length(y), 0, 1)), |
| 159 | + ['a', 'abcd', 'abc']) | |
| 160 | ++--------------------------------------------------------------------------------------------------------------------------------------------------------------------+ |
| 161 | +| ["a", "abc", "abcd"] | |
| 162 | ++--------------------------------------------------------------------------------------------------------------------------------------------------------------------+ |
| 163 | +``` |
| 164 | + |
| 165 | +```sql |
| 166 | +SELECT array_sort((x, y) -> IF(cardinality(x) < cardinality(y), -1, |
| 167 | + IF(cardinality(x) = cardinality(y), 0, 1)), |
| 168 | + [[2, 3, 1], [4, 2, 1, 4], [1, 2]]); |
| 169 | +``` |
| 170 | + |
| 171 | +```text |
| 172 | ++-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ |
| 173 | +| array_sort((x, y) -> IF(cardinality(x) < cardinality(y), -1, |
| 174 | + IF(cardinality(x) = cardinality(y), 0, 1)), |
| 175 | + [[2, 3, 1], [4, 2, 1, 4], [1, 2]]) | |
| 176 | ++-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ |
| 177 | +| [[1, 2], [2, 3, 1], [4, 2, 1, 4]] | |
| 178 | ++-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ |
| 179 | +``` |
0 commit comments