After array
b is sorted in ascending order, it becomes [5,6,7]. Array a becomes [1,4,3] accordingly.
when the number of arrays involved in sorting is greater than two,The purpose of this function is to sort array0 based on the values in multiple array columns (such as array1, array2, array3, etc.). The sorting rules are as follows:
First, compare the corresponding elements in array1;
If they are the same, compare the corresponding elements in array2;
And so on, until the last array column.
Note:
The elements in the arrays participating in the sort must be of a sortable type or JSON type.
The size of the arrays participating in the sort must be consistent with the original array (except for NULL values).
Example Explanation
Given the following four arrays:
- Compare array1:
- The sorted index order of array1 is [4, 2, 3, 1, 0] because 4 < 5 < 5 < 5 < 6.
- For the elements [5, 5, 5] in array1, compare array2:
- The sorted order of [a, b, b] in array2 is [2, 3, 4] because a < b = b.
- For the elements [b, b] in array1 and array2, compare array3:
- The sorted order of [2023-01-04, 2023-01-05] in array3 is [2, 4] because 2023-01-04 < 2023-01-05.
Syntax
-
array_sortby(array0, array1)Sortsarray0according to the ascending order ofarray1. -
array_sortby(<lambda function>, array0 [, array1...])Sortsarray0according to the array returned from the lambda function. -
array_sortby(array0, array1, [array2, array3...])Sort array0 in ascending order based on the values of multiple array columns (such as array1, array2, array3, etc.). The sorting rule is: first, compare the corresponding elements of array1; if they are the same, compare the corresponding elements of array2; and so on, until the last array column.
Parameters
array0: the array you want to sort. It must be an array, array expression, ornull. Elements in the array must be sortable.array1: the sorting array used to sortarray0. It must be an array, array expression, ornull.lambda function: the lambda expression used to generate the sorting array.array1, [array2, array3...]:the sorting array used to sortarray0. It must be an array, array expression, ornull.
Return value
Returns an array.Usage notes
- This function can sort elements of an array only in ascending order.
NULLvalues are placed at the beginning of the array that is returned.- If you want to sort elements of an array in descending order, use the reverse function.
- If the sorting array (
array1) is null, data inarray0remains unchanged. - The elements of the returned array have the same data type as the elements of
array0. The attribute of null values are also the same. - All arrays must have the same number of elements. Otherwise, an error is returned.
Examples
The following table is used to demonstrate how to use this function.c3 according to c2. This example also provides the result of array_sort() for comparison.
c3 based on c2 generated from a lambda expression. This example is equivalent to Example 1. It also provides the result of array_sort() for comparison.
c3 based on the ascending order of c2+c3.
array_col1 according to array_col2, array_col3.
array_col1 according to array_col2, array_col3, array_col4.