# 3.4 排序相关命令

## 3.4.1 用`SORT`命令进行排序

* `SORT`
  * 语法:`SORT key [BY pattern] [LIMIT offset count] [GET pattern [GET pattern ...]] [ASC|DESC] [ALPHA] [STORE destination]`,其中:
    * BY:指定排序模式
    * LIMIT offset count:
      * offset:偏移量
      * count:需返回的元素个数
    * GET:以排序的结果作为键名,去获取这些键名对应的值
    * ASC/DESC:指定排序规则为升/降序
    * ALPHA:该选项用于指定对一个包含字符串值的集合键进行排序
    * STORE:默认状况下,排序结果会返回给客户端.使用STORE描述符,能够将结果存储在指定key上,若是Key已经存在则覆盖.而不将排序结果返回给客户端
  * 功能:升序或降序排列

例:使用`SORT`命令对list进行升序排列

创建list:

```
127.0.0.1:6379> LPUSH salary 10000 15000 13500 12000
(integer) 4
```

读取list时升序排列:

```
127.0.0.1:6379> SORT salary ASC
1) "10000"
2) "12000"
3) "13500"
4) "15000"
```

按写入顺序读取list:

```
127.0.0.1:6379> LRANGE salary 0 -1 
1) "12000"
2) "13500"
3) "15000"
4) "10000"
```

例:使用`SORT`命令对set进行降序排列

创建set:

```
127.0.0.1:6379> SADD name 'Peter' 'Tom' 'Mary'
(integer) 3
```

降序排序读取:

```
127.0.0.1:6379> SORT name DESC
(error) ERR One or more scores can't be converted into double
```

可以看到,`SORT`命令默认只能对数值型元素进行排序

使用`SORT`命令对字符串类型元素进行排序:

```
127.0.0.1:6379> SORT name DESC ALPHA
1) "Tom"
2) "Peter"
3) "Mary"
```

例:使用`SORT`命令对zset排序

创建zset:

```
127.0.0.1:6379> ZADD nameSet 4.0 Mike 2.0 Peter 1.0 Tim 0.5 Johnson
(integer) 4
```

按zset中元素的ASCII码升序排序:

```
127.0.0.1:6379> SORT nameSet ASC ALPHA
1) "Johnson"
2) "Mike"
3) "Peter"
4) "Tim"
```

按zset中元素的ASCII码降序排序:

```
127.0.0.1:6379> SORT nameSet DESC ALPHA
1) "Tim"
2) "Peter"
3) "Mike"
4) "Johnson"
```

可以看到,使用`SORT`命令读取zset时,排序与元素的score是无关的,仅与元素的字面量有关

## 3.4.2 用`BY`参数指定排序模式

例:

现有一list如下:

```
127.0.0.1:6379> LPUSH vipLevel VIP1 VIP3 VIP2
(integer) 3
```

按VIP后的数字降序排序:

```
127.0.0.1:6379> SORT vipLevel DESC BY VIP*
1) "VIP3"
2) "VIP2"
3) "VIP1"
```

## 3.4.3 用`LIMIT`参数返回部分排序结果

例:

创建list:

```
127.0.0.1:6379> RPUSH number 1 3 2 4 6 5 8 7
(integer) 8
```

升序排序取前3个元素:

```
127.0.0.1:6379> SORT number LIMIT 0 3 ASC
1) "1"
2) "2"
3) "3"
```

升序排序取第5个元素和第6个元素的值:

```
127.0.0.1:6379> SORT number LIMIT 4 2 ASC
1) "5"
2) "6"
```

## 3.4.4 `SORT`命令里`GET`参数的用法

`GET`参数:以排序的结果作为键名,去获取和这些键名相关的值

例:

创建一个list:

```
127.0.0.1:6379> LPUSH score 100 80 90 85
(integer) 4
```

以该list中的元素为键名,创建与这些键名有关的字符串:

创建键名以`name-`开头的且和list中的元素有关的字符串:

```
127.0.0.1:6379> SET name-100 Peter-100
OK
127.0.0.1:6379> SET name-80 Mary-80
OK
```

创建键名以`symbol-`开头且和list中的元素有关的字符串:

```
127.0.0.1:6379> SET symbol-90 RMB-90
OK
127.0.0.1:6379> SET symbol-85 USD-85
OK
```

以升序对list排序,获取键名为`name-list中的元素`的值:

```
127.0.0.1:6379> SORT score GET name-* ASC 
1) "Mary-80"
2) (nil)
3) (nil)
4) "Peter-100"
```

可以看到,由于存在键名为`name-100`和`name-80`的值,故能够取到

以降序对list排序,获取键名为`symbol-list中的元素`的值:

```
127.0.0.1:6379> SORT score GET symbol-* DESC
1) (nil)
2) "RMB-90"
3) "USD-85"
4) (nil)
```

以降序对list排序,获取键名为键名为`name-list中的元素`和键名为`symbol-list中的元素`的值:

```
127.0.0.1:6379> SORT score GET name-* GET symbol-* DESC
1) "Peter-100"	// name-100
2) (nil)			// symbol-100
3) (nil)			// name-90
4) "RMB-90"		// symbol-90
5) (nil)			// name-85
6) "USD-85"		// symbol-85
7) "Mary-80"		// name-80
8) (nil)			// symbol-80
```

## 3.4.5 通过`STORE`参数提升性能

例:将list中的元素倒序排序后保存为另一个list

查看list:

```
127.0.0.1:6379> LRANGE score 0 -1
1) "85"
2) "90"
3) "80"
4) "100"
```

将list倒序排序并保存为一个名为score-desc的list:

```
127.0.0.1:6379> SORT score DESC STORE score-desc
(integer) 4
```

查看保存的结果:

```
127.0.0.1:6379> LRANGE score-desc 0 -1
1) "100"
2) "90"
3) "85"
4) "80"
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://redis.sai.show/di-3-zhang-shi-jian-redis-de-chang-yong-ming-ling/3.4-pai-xu-xiang-guan-ming-ling.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
