|
2 | 2 | title: ARRAY_SORT
|
3 | 3 | ---
|
4 | 4 |
|
5 |
| -Sorts elements in the array in ascending order. |
| 5 | +Sorts the elements in an array in ascending order. |
6 | 6 |
|
7 | 7 | ## Syntax
|
8 | 8 |
|
9 | 9 | ```sql
|
10 |
| -ARRAY_SORT( <array>[, <order>, <nullposition>] ) |
| 10 | +ARRAY_SORT(<array>[, 'ASC'|'DESC', 'NULLS FIRST'|'NULLS LAST']) |
11 | 11 | ```
|
12 | 12 |
|
13 |
| -| Parameter | Default | Description | |
14 |
| -|--------------|-------------|------------------------------------------------------------------------------------------------------------------------------------------------| |
15 |
| -| order | ASC | Specifies the sorting order as either ascending (ASC) or descending (DESC). | |
16 |
| -| nullposition | NULLS FIRST | Determines the position of NULL values in the sorting result, at the beginning (NULLS FIRST) or at the end (NULLS LAST) of the sorting output. | |
| 13 | +| Parameter | Default? | Description | |
| 14 | +|---------------|----------|----------------------------------------------------------------------------------------------| |
| 15 | +| `ASC` | Yes | Specifies that the sorting order is ascending, arranging elements from smallest to largest. | |
| 16 | +| `DESC` | No | Specifies that the sorting order is descending, arranging elements from largest to smallest. | |
| 17 | +| `NULLS FIRST` | Yes | Determines that NULL values should appear at the beginning of the sorted output. | |
| 18 | +| `NULLS LAST` | No | Determines that NULL values should appear at the end of the sorted output. | |
17 | 19 |
|
18 | 20 | ## Examples
|
19 | 21 |
|
| 22 | +This example shows the array sorted in ascending order by default, with the elements arranged from smallest to largest: |
| 23 | + |
20 | 24 | ```sql
|
21 | 25 | SELECT ARRAY_SORT([1, 4, 3, 2]);
|
22 | 26 |
|
23 |
| -┌──────────────────────────┐ |
24 |
| -│ array_sort([1, 4, 3, 2]) │ |
25 |
| -├──────────────────────────┤ |
26 |
| -│ [1,2,3,4] │ |
27 |
| -└──────────────────────────┘ |
| 27 | +-[ RECORD 1 ]----------------------------------- |
| 28 | +array_sort([1, 4, 3, 2]): [1,2,3,4] |
| 29 | +``` |
| 30 | + |
| 31 | +This example sorts the array in descending order, arranging the elements from largest to smallest: |
| 32 | + |
| 33 | +```sql |
| 34 | +SELECT ARRAY_SORT([1, 4, 3, 2], 'DESC'); |
| 35 | + |
| 36 | +-[ RECORD 1 ]----------------------------------- |
| 37 | +array_sort([1, 4, 3, 2], 'desc'): [4,3,2,1] |
| 38 | +``` |
| 39 | + |
| 40 | +This example demonstrates how NULL values are handled in descending order, with NULLs appearing before all non-NULL values: |
| 41 | + |
| 42 | +```sql |
| 43 | +SELECT ARRAY_SORT([1, 4, 3, 2, NULL], 'DESC'); |
| 44 | + |
| 45 | +-[ RECORD 1 ]----------------------------------- |
| 46 | +array_sort([1, 4, 3, 2, null], 'desc'): [NULL,4,3,2,1] |
| 47 | +``` |
| 48 | + |
| 49 | +This example shows the array sorted in descending order while ensuring that NULL values appear at the end of the sorted output: |
| 50 | + |
| 51 | +```sql |
| 52 | +SELECT ARRAY_SORT([1, 4, 3, 2, NULL], 'DESC', 'NULLS LAST'); |
| 53 | + |
| 54 | +-[ RECORD 1 ]----------------------------------- |
| 55 | +array_sort([1, 4, 3, 2, null], 'desc', 'nulls last'): [4,3,2,1,NULL] |
28 | 56 | ```
|
0 commit comments