Redis大总结之三:SORT命令(对 列表|集合|有序集合 进行排序)

SORT命令

 列表|集合|有序集合 进行排序

对列表进行排序:

127.0.0.1:6379[5]> lrange myList 0 -1

1) "1"

2) "2"

3) "4"

4) "3"

5) "5"

127.0.0.1:6379[5]> sort myList

1) "1"

2) "2"

3) "3"

4) "4"

5) "5"

127.0.0.1:6379[5]> lrange myList 0 -1

1) "1"

2) "2"

3) "4"

4) "3"

5) "5"

 

对有序集合进行安排续:会忽略有序几个元素的分数,只针对元素键排序,加上alpha参数。

127.0.0.1:6379[5]> zadd set5 30 a 10 b 20 c 5 d

(integer) 4

127.0.0.1:6379[5]> zrange set5 0 -1 withscores

1) "d"

2) "5"

3) "b"

4) "10"

5) "c"

6) "20"

7) "a"

8) "30"

127.0.0.1:6379[5]> sort set5

(error) ERR One or more scores can't be converted into double

127.0.0.1:6379[5]> sort set5 alpha

1) "a"

2) "b"

3) "c"

4) "d"

 

对集合进行排序:

127.0.0.1:6379[5]> sadd set2 d a c b

(integer) 4

127.0.0.1:6379[5]> smembers set2

1) "c"

2) "a"

3) "b"

4) "d"

127.0.0.1:6379[5]> sort set2

(error) ERR One or more scores can't be converted into double

127.0.0.1:6379[5]> sort set2 alpha

1) "a"

2) "b"

3) "c"

4) "d"

 

SORT命令之DESC参数:

对列表从大到小排序:

127.0.0.1:6379[5]> lrange myList 0 -1

1) "1"

2) "2"

3) "4"

4) "3"

5) "5"

127.0.0.1:6379[5]> sort myList DESC

1) "5"

2) "4"

3) "3"

4) "2"

5) "1"

对集合从大到小排序:

127.0.0.1:6379[5]> smembers set3

1) "20"

2) "50"

3) "100"

127.0.0.1:6379[5]> sort set3 desc

1) "100"

2) "50"

3) "20"

 

对有序集合进行排序:

127.0.0.1:6379[5]> zrange set5  0 -1 withscores

1) "d"

2) "5"

3) "b"

4) "10"

5) "c"

6) "20"

7) "a"

8) "30"

127.0.0.1:6379[5]> sort set5 desc

(error) ERR One or more scores can't be converted into double

127.0.0.1:6379[5]> sort set5 alpha desc

1) "d"

2) "c"

3) "b"

4) "a"

 

SORT命令之LIMIT参数:

127.0.0.1:6379[5]> sort set3 desc

1) "100"

2) "50"

3) "20"

127.0.0.1:6379[5]> sort set3 desc limit 1 2  //向后偏移1个元素,获取两个元素

1) "50"

2) "20"

 

SORT命令之BY,GET参数:

127.0.0.1:6379[6]> lpush list 5 3 9

(integer) 3

127.0.0.1:6379[6]> lrange list 0 -1

1) "9"

2) "3"

3) "5"

127.0.0.1:6379[6]> set it:5 30

OK

127.0.0.1:6379[6]> set it:9 40

OK

127.0.0.1:6379[6]> set it:3 90

OK

127.0.0.1:6379[6]> sort list by it:* desc

1) "3"

2) "9"

3) "5"

127.0.0.1:6379[7]> sort list by it:* desc get it:*//结果显示it:3,it:9,:it:5对应的值

1) "90"

2) "40"

3) "30"

//解析:如上所示:sortlist列表进行排序,使用了by参数,那么就不是使用list中的值来排序了,而是将list中的值[9,3,5]替换到[it:*]这个*,替换关系:list[9,3,5]==[it:9,it:3,it:5],然后对[it:9,it:3,it:5]这三个元素排序,以上可知:[it:9,it:3,it:5]对应的值为[it:9=40,it:3=90,it:5=30],倒叙排列对应的关系即为[it:3=90,it:9=40,it:5=30],对应的[it:3=90,it:9=40,it:5=30] == list[3,9,5],即得到list列表的顺序。

 

SORT命令之STORE参数:表示把sort查询的结果集保存起来

127.0.0.1:6379[7]> sort list by it:* desc get it:*

1) "90"

2) "50"

3) "40"

127.0.0.1:6379[7]> sort list by it:* desc get it:* store sorc:result//保存到sorc:result

(integer) 3

127.0.0.1:6379[7]> lrange sorc:result 0 -1

1) "90"

2) "50"

3) "40"

 

更多:

Redis大总结之一:Redis 五大数据类型及操作


 ------------------完------------------------


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值