> ## Documentation Index
> Fetch the complete documentation index at: /documents/llms.txt
> Use this file to discover all available pages before exploring further.

# array_sort

> 按升序对数组元素进行排序，v4.0开始支持Lambda比较器。

按升序对数组元素进行排序。从 v4.0 开始，`array_sort` 支持 Lambda Comparator。

## 语法

```Haskell theme={null}
-- 不使用 Lambda Comparator
array_sort(array)

-- 使用 Lambda Comparator
array_sort(array, (x,y)->expr(x,y))
```

## 参数

`array`：要排序的数组元素。仅支持 ARRAY 数据类型。

`lambda_comparator`：一个可选的 lambda 函数比较器，格式为 `(x,y)->expr(x,y)`。lambda Comparator 必须满足以下条件：

* 必须只依赖于参数 x 和 y
* 必须返回布尔或数字类型
* 结果不应为 NULL
* 必须满足严格弱序要求

## 返回值

返回一个数组。

## 使用说明

### 基本排序

* 当不使用 lambda Comparator 时，此函数按升序对数组元素进行排序。
* `NULL` 值放置在返回数组的开头。
* 返回数组的元素与输入数组的元素具有相同的数据类型。

### Lambda Comparator 排序

* 使用 lambda Comparator 时，排序顺序由表达式结果决定：
  * 对于布尔返回值：`true` 表示 x 应在 y 之前
  * 对于数字返回值：负数表示 x 在 y 之前

* lambda Comparator 必须满足严格弱序要求：
  * **非自反性**: 对于所有 `x`，`expr(x, x)` 必须返回 false（布尔）或非负数（数字）
  * **不对称性**: 如果 `expr(x, y)` 成立，则 `expr(y, x)` 不得成立
  * **传递性**: 如果 `expr(x, y)` 和 `expr(y, z)` 成立，则 `expr(x, z)` 必须成立
  * **连通性**: 对于所有 `x` 和 `y`，以下之一必须为真：
    * `expr(x, y)`
    * `expr(y, x)`
    * `x = y`

* lambda Comparator 表达式必须：
  * 只依赖于参数 `x` 和 `y`
  * 返回布尔或数字类型
  * 从不返回 NULL
  * 始终为相同的输入值生成一致的结果

### 降序

如果要按降序对数组元素进行排序，请使用 [reverse](/documents/cn/4.2/sql-reference/sql-functions/string-functions/reverse.md) 函数。

## 示例

### 基本示例

下表用作示例：

```plaintext theme={null}
mysql> select * from test;
+------+--------------+
| c1   | c2           |
+------+--------------+
|    1 | [4,3,null,1] |
|    2 | NULL         |
|    3 | [null]       |
|    4 | [8,5,1,4]    |
+------+--------------+
```

按列 `c2` 的值按升序排序。

```plaintext theme={null}
mysql> select c1, array_sort(c2) from test;
+------+------------------+
| c1   | array_sort(`c2`) |
+------+------------------+
|    1 | [null,1,3,4]     |
|    2 | NULL             |
|    3 | [null]           |
|    4 | [1,4,5,8]        |
+------+------------------+
```

### Lambda Comparator 示例

使用 lambda Comparator 按降序对整数数组进行排序：

```plaintext theme={null}
mysql> select array_sort([3,1,4,2], (x,y) -> x > y);
+-------------------------------------------+
| array_sort([3, 1, 4, 2], (x, y) -> x > y) |
+-------------------------------------------+
| [4, 3, 2, 1]                              |
+-------------------------------------------+
```

使用数值返回值对整数数组进行排序：

```plaintext theme={null}
mysql> select array_sort([3,1,4,2], (x,y) -> x - y);
+-------------------------------------------+
| array_sort([3, 1, 4, 2], (x, y) -> x - y) |
+-------------------------------------------+
| [1, 2, 3, 4]                              |
+-------------------------------------------+
```

按长度对字符串数组进行排序：

```plaintext theme={null}
mysql> select array_sort(['apple', 'banana', 'cherry', 'date'], (x,y) -> length(x) - length(y));
+------------------------------------------------------------------------------------+
| array_sort(['apple', 'banana', 'cherry', 'date'], (x, y) -> length(x) - length(y)) |
+------------------------------------------------------------------------------------+
| ['date', 'apple', 'banana', 'cherry']                                              |
+------------------------------------------------------------------------------------+
```

使用布尔返回值按字母顺序对字符串数组进行排序：

```plaintext theme={null}
mysql> select array_sort(['banana', 'apple', 'cherry'], (x,y) -> x > y);
+------------------------------------------------------------+
| array_sort(['banana', 'apple', 'cherry'], (x, y) -> x > y) |
+------------------------------------------------------------+
| ['apple', 'banana', 'cherry']                              |
+------------------------------------------------------------+
```

使用自定义业务逻辑对数组进行排序（先排序偶数，后排序奇数）：

```plaintext theme={null}
mysql> select array_sort([5,2,8,1,9,4], (x,y) -> 
    case when x % 2 = 0 and y % 2 = 0 then x - y
         when x % 2 = 0 and y % 2 = 1 then -1
         when x % 2 = 1 and y % 2 = 0 then 1
         else x - y end);
+--------------------------------------------------------------------------------------------------------------------+
| array_sort([5, 2, 8, 1, 9, 4], (x, y) -> case when x % 2 = 0 and y % 2 = 0 then x - y when x % 2 = 0 and y % 2 = 1 then -1 when x % 2 = 1 and y % 2 = 0 then 1 else x - y end) |
+--------------------------------------------------------------------------------------------------------------------+
| [2, 4, 8, 1, 5, 9]                                                                                                 |
+--------------------------------------------------------------------------------------------------------------------+
```

使用自定义精度比较对小数数组进行排序：

```plaintext theme={null}
mysql> select array_sort([3.141, 2.718, 1.414, 2.236], (x,y) -> round(x, 2) - round(y, 2));
+----------------------------------------------------------------------------------------+
| array_sort([3.141, 2.718, 1.414, 2.236], (x, y) -> round(x, 2) - round(y, 2))          |
+----------------------------------------------------------------------------------------+
| [1.414, 2.236, 2.718, 3.141]                                                           |
+----------------------------------------------------------------------------------------+
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.