Note that `arraySort` is a [higher-order function](higher_order_functions.md). You can pass a lambda function to it as the first argument. In this case, sorting order is determined by the result of the lambda function applied to the elements of the array. Example is shown below.
Note that `arraySort` is a [higher-order function](higher_order_functions.md). You can pass a lambda function to it as the first argument. In this case, sorting order is determined by the result of the lambda function applied to the elements of the array.
Let's consider the following example:
``` sql
SELECTarraySort((x)->-x,[1,2,3])asres;
...
...
@@ -527,7 +529,7 @@ SELECT arraySort((x, y) -> y, ['hello', 'world'], [2, 1]) as res;
Here, the elements that are passed in the second array ([2, 1]) define a new position of each corresponding element from the source array (['hello', 'world']), that is, ['hello' –> 2, 'world' –> 1]. So, 'hello' will be the second element in a result, and 'world' will be the first.
Another example is shown below. In this example, sorting order is specified by the string values:
Note that the `arrayReverseSort` is a [higher-order function](higher_order_functions.md). You can pass a lambda function to it as the first argument.
Let's consider the following example:
Note that the `arrayReverseSort` is a [higher-order function](higher_order_functions.md). You can pass a lambda function to it as the first argument. Example is shown below.
``` sql
SELECTarrayReverseSort((x)->-x,[1,2,3])asres;
...
...
@@ -617,7 +626,7 @@ In this example, the array is sorted in the following way:
1. At first, the source array (['hello', 'world']) is sorted according to the result of the lambda function applied to the elements of the arrays. The elements that are passed in the second array ([2, 1]), define a new position of corresponding elements from the source array. The result is an array ['world', 'hello'].
2. Array that was sorted on the previous step, is reversed. So, the final result is ['hello', 'world'].
Another example is shown below. In this example, sorting order is specified by the string values:
@@ -529,7 +529,7 @@ SELECT arraySort((x, y) -> y, ['hello', 'world'], [2, 1]) as res;
Элементы, указанные во втором массиве ([2,1]), определяют позицию соответствующих элементов из исходного массива (['hello', 'world']), то есть ['hello' –> 2, 'world' –> 1]. Таким образом, 'hello' будет вторым элементом в отсортированном массиве, а 'world' — первым.
Ниже приведен ещё один пример, в котором порядок сортировки устанавливается строковыми значениями:
@@ -539,6 +539,14 @@ SELECT arraySort((x, y) -> y, [0, 1, 2], ['c', 'b', 'a']) as res;
│[2,1,0]│
└─────────┘
```
``` sql
SELECTarraySort((x,y)->-y,[0,1,2],[1,2,3])asres;
```
``` sql
┌─res─────┐
│[2,1,0]│
└─────────┘
```
!!! note "Примечание"
Для улучшения эффективности сортировки применяется [Преобразование Шварца](https://ru.wikipedia.org/wiki/%D0%9F%D1%80%D0%B5%D0%BE%D0%B1%D1%80%D0%B0%D0%B7%D0%BE%D0%B2%D0%B0%D0%BD%D0%B8%D0%B5_%D0%A8%D0%B2%D0%B0%D1%80%D1%86%D0%B0).
...
...
@@ -601,7 +609,7 @@ SELECT arrayReverseSort((x, y) -> y, ['hello', 'world'], [2, 1]) as res;
1. Сначала массив сортируется в том порядке, который определяется лямбда-функцией. Элементы, указанные во втором массиве ([2,1]), определяют позицию соответствующих элементов из исходного массива (['hello', 'world']). То есть, промежуточным результатом будет массив ['world', 'hello'].
2. Массив, который был отсортирован на предыдущем шаге, переворачивается. Получается массив ['hello', 'world'].
Ниже приведён еще один пример, в котором порядок сортировки устанавливается строковыми значениями: