Skip to content

Commit bf15b34

Browse files
committed
Update array-sort.md
1 parent e6a0e3f commit bf15b34

File tree

1 file changed

+39
-11
lines changed
  • docs/en/sql-reference/20-sql-functions/00-array-functions

1 file changed

+39
-11
lines changed

docs/en/sql-reference/20-sql-functions/00-array-functions/array-sort.md

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,55 @@
22
title: ARRAY_SORT
33
---
44

5-
Sorts elements in the array in ascending order.
5+
Sorts the elements in an array in ascending order.
66

77
## Syntax
88

99
```sql
10-
ARRAY_SORT( <array>[, <order>, <nullposition>] )
10+
ARRAY_SORT(<array>[, 'ASC'|'DESC', 'NULLS FIRST'|'NULLS LAST'])
1111
```
1212

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. |
1719

1820
## Examples
1921

22+
This example shows the array sorted in ascending order by default, with the elements arranged from smallest to largest:
23+
2024
```sql
2125
SELECT ARRAY_SORT([1, 4, 3, 2]);
2226

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]
2856
```

0 commit comments

Comments
 (0)