Skip to main content
Sorts elements in an array according to the ascending order of elements in another array or array converted from a lambda expression. For more information, see Lambda expression. This function is supported from v2.5. Elements in the two arrays are like key-value pairs. For example, b = [7,5,6] is the sorting key of a = [3,1,4]. According to the key-value pair relationship, elements in the two arrays have the following one-to-one mapping. 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:
Sorting Steps:
  1. Compare array1:
  • The sorted index order of array1 is [4, 2, 3, 1, 0] because 4 < 5 < 5 < 5 < 6.
  1. 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.
  1. 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.
Final Sorting Result for array0:

Syntax

  • array_sortby(array0, array1) Sorts array0 according to the ascending order of array1.
  • array_sortby(<lambda function>, array0 [, array1...]) Sorts array0 according 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, or null. Elements in the array must be sortable.
  • array1: the sorting array used to sort array0. It must be an array, array expression, or null.
  • lambda function: the lambda expression used to generate the sorting array.
  • array1, [array2, array3...]:the sorting array used to sort array0. It must be an array, array expression, or null.

Return value

Returns an array.

Usage notes

  • This function can sort elements of an array only in ascending order.
  • NULL values 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 in array0 remains 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.
Example 1: Sort c3 according to c2. This example also provides the result of array_sort() for comparison.
Example 2: Sort array 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.
Example 3: Sort array c3 based on the ascending order of c2+c3.
Example 1: Sort array_col1 according to array_col2, array_col3.
Example 2: Sort array_col1 according to array_col2, array_col3, array_col4.

References

array_sort