List sorting and reversing functions
Use these functions to sort a list in ascending or descending order, to reverse its order, or to get the positions of its elements in sorted order. When you sort, you can choose whether NULL elements come first or last.
array_reverse function
Returns the elements of list in reverse order. NULL elements are kept and move with the other elements. An empty list returns an empty list, and if list is NULL, the result is NULL.
For a list of lists, only the outer list is reversed, and each inner list keeps its order. If you pass an ARRAY value, the result is a LIST. array_reverse returns the same result as list_reverse.
Syntax
array_reverse(list)
Arguments
The array_reverse function takes the following argument:
list-
The list to reverse.
Return type
A list of the element type of list, such as INTEGER[].
Examples
The following SQL query reverses a list.
SELECT array_reverse([4, 5, 6]) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query reverses a list of strings that has a NULL element.
SELECT array_reverse(['a', NULL, 'c']) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query reverses a list of lists. The inner lists keep their order.
SELECT array_reverse([[1, 2], [3]]) AS result
The query returns the following result.
| result |
|---|
|
Considerations
array_reverse does not accept a string. To reverse the characters of a string, use the reverse function.
Differences from Athena engine version 3
In Athena engine version 3, reverse also reverses an array, as in reverse(ARRAY[1, 2, 3]). In DuckDB, reverse accepts only strings. To reverse a list, use array_reverse or list_reverse.
list_grade_up function
Returns a list of positions in list, arranged so that reading the elements at those positions gives the elements in sorted order. Positions start at 1. The first number in the result is the position of the smallest element, or of the largest element with 'DESC'. The second number is the position of the next element, and so on. The sort order is the one that list_sort uses, but the function returns positions instead of values. Elements that are equal keep their original order.
By default, the sort is ascending and NULL elements sort last. If list, sort_order, or null_order is NULL, the result is NULL.
array_grade_up and grade_up are aliases of list_grade_up.
Syntax
list_grade_up(list [, sort_order [, null_order ] ])
Arguments
The list_grade_up function takes the following arguments:
list-
The list whose sort order you want, with elements of any type that can be sorted.
sort_order-
The sort direction, as a
VARCHARvalue:'ASC'for ascending order or'DESC'for descending order, in any letter case. The default is'ASC'. This argument is optional. null_order-
The sort position of
NULLelements, as aVARCHARvalue:'NULLS FIRST'or'NULLS LAST', in any letter case. The default is'NULLS LAST'. This argument is optional.
Return type
BIGINT[]
Examples
The following SQL query returns the positions that sort a list in ascending order. The smallest value, 1, is at position 3, the next value, 2, is at position 4, and so on.
SELECT list_grade_up([3, 6, 1, 2]) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query returns the positions for descending order.
SELECT list_grade_up([3, 6, 1, 2], 'DESC') AS result
The query returns the following result.
| result |
|---|
|
The following SQL query shows that the position of the NULL element, 2, comes last in the result by default and first with 'NULLS FIRST'.
SELECT list_grade_up([3, NULL, 1]) AS nulls_last, list_grade_up( [3, NULL, 1], 'ASC', 'NULLS FIRST' ) AS nulls_first
The query returns the following result.
| nulls_last | nulls_first |
|---|---|
|
|
The following SQL query sorts a list of names by a second list of amounts. list_select returns the names at the positions that list_grade_up returns for the amounts.
SELECT list_select( ['carol', 'alice', 'bob'], list_grade_up([42, 120, 75]) ) AS result
The query returns the following result.
| result |
|---|
|
Considerations
The result lists positions, not the rank of each element. For example, list_grade_up([30, 10, 20]) returns [2, 3, 1], because the smallest value is at position 2. To get the rank of each element instead, apply the function to its own result: list_grade_up(list_grade_up([30, 10, 20])) returns [3, 1, 2].
Use 'ASC' or 'DESC' for sort_order, and 'NULLS FIRST' or 'NULLS LAST' for null_order. A value that the function does not recognize, such as 'UP', makes the query fail.
list_reverse function
Returns the elements of list in reverse order, so the last element comes first. NULL elements are kept, in their reversed positions. If list is NULL, the result is NULL.
list_reverse accepts only lists. To reverse the characters of a string, use the reverse function.
Syntax
list_reverse(list)
Arguments
The list_reverse function takes the following argument:
list-
The list to reverse, of any element type.
Return type
A list of the same type as list, for example INTEGER[].
Examples
The following SQL query reverses a list.
SELECT list_reverse([4, 5, 6]) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query reverses a list that has a NULL element. The NULL element stays in the result.
SELECT list_reverse(['a', NULL, 'c']) AS result
The query returns the following result.
| result |
|---|
|
Differences from Athena engine version 3
In Athena engine version 3, reverse also accepts an array and reverses its elements. In DuckDB, reverse accepts only strings, so a call such as reverse([1, 2, 3]) fails. Use list_reverse to reverse a list.
list_reverse_sort function
Returns the elements of list sorted from largest to smallest. By default, NULL elements go at the end of the result. To put them at the start, pass 'NULLS FIRST' as null_order.
list_reverse_sort( returns the same result as list)list_sort(. If list, 'DESC')list is NULL, the result is NULL. array_reverse_sort is an alias of list_reverse_sort.
Syntax
list_reverse_sort(list [, null_order ])
Arguments
The list_reverse_sort function takes the following arguments:
list-
The list to sort. Its elements can be of any type that can be ordered.
null_order-
The sort position of
NULLelements:'NULLS FIRST'or'NULLS LAST'. The default is'NULLS LAST'. The value is not case-sensitive. This argument is optional.
Return type
A list of the same type as list, for example INTEGER[].
Examples
The following SQL query sorts a list in descending order.
SELECT list_reverse_sort([3, 6, 1, 2]) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query sorts a list that has a NULL element. By default, the NULL element goes last.
SELECT list_reverse_sort([3, NULL, 1, 2]) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query puts the NULL element first.
SELECT list_reverse_sort( [3, NULL, 1, 2], 'NULLS FIRST' ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query uses the array_reverse_sort alias on strings. The lowercase letters come before the uppercase B.
SELECT array_reverse_sort(['b', 'a', 'B']) AS result
The query returns the following result.
| result |
|---|
|
Considerations
The second argument sets only where NULL elements go. The order is always descending, and a sort order such as 'DESC' in the second argument makes the query fail.
Strings are compared by their character codes, so in descending order lowercase letters come before uppercase letters.
list_sort function
Returns the elements of list in sorted order. By default, the order is ascending, and NULL elements go at the end of the result. To change the order, pass sort_order. To change where NULL elements go, also pass null_order.
If list is NULL, the result is NULL. array_sort is an alias of list_sort. To sort in descending order, you can also use the list_reverse_sort function.
Syntax
list_sort(list [, sort_order [, null_order ] ])
Arguments
The list_sort function takes the following arguments:
list-
The list to sort. Its elements can be of any type that can be ordered, including lists and structs.
sort_order-
The sort direction:
'ASC'for ascending order or'DESC'for descending order. The default is'ASC'. The value is not case-sensitive. This argument is optional. null_order-
The sort position of
NULLelements:'NULLS FIRST'or'NULLS LAST'. The default is'NULLS LAST'. The value is not case-sensitive. This argument is optional.
Return type
A list of the same type as list, for example INTEGER[].
Examples
The following SQL query sorts a list in ascending order.
SELECT list_sort([3, 6, 1, 2]) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query sorts a list in descending order.
SELECT list_sort([3, 6, 1, 2], 'DESC') AS result
The query returns the following result.
| result |
|---|
|
The following SQL query uses the array_sort alias on a list that has a NULL element. By default, the NULL element goes last.
SELECT array_sort([3, NULL, 1, 2]) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query sorts in descending order and puts the NULL element first.
SELECT list_sort( [3, NULL, 1, 2], 'DESC', 'NULLS FIRST' ) AS result
The query returns the following result.
| result |
|---|
|
Considerations
To pass null_order, also pass sort_order, as in list_sort(. A call such as list, 'ASC', 'NULLS FIRST')list_sort( fails, because the second argument is always the sort order.list, 'NULLS FIRST')
list_sort does not accept a comparison lambda. To sort a list by a computed key or by the values of another list, use the list_select function with list_grade_up.
Strings are compared by their character codes, so in ascending order uppercase letters come before lowercase letters.
Differences from Athena engine version 3
In Athena engine version 3, array_sort accepts a comparison function as its second argument, such as array_sort(ARRAY[3, 1, 2], (x, y) -> IF(x < y, 1, IF(x = y, 0, -1))). In DuckDB, array_sort is an alias of list_sort, which does not accept a function. To sort in descending order, pass 'DESC' or use the list_reverse_sort function.