Redis-排序

Sort命令可以对列表、集合和有序集合进行排序。

命令

sort list     #对列表中的数据进行排序
sort list alpha   #按照字典顺序排序
sort list desc    #倒排序
sort list limit index offset  #截取返回的数据
sort list by hlist.field    #根据散列中的某个字段值排序  
sort list by hlist.field get hlist.filed #将排序好的list中的元素转成另一个值
127.0.0.1:6379> lpush list1 2 3 6 1 56 34 1
(integer) 7
127.0.0.1:6379> sort list1
1) "1"
2) "1"
3) "2"
4) "3"
5) "6"
6) "34"
7) "56"
127.0.0.1:6379> zadd list 3 4 6 7 3 2 3
(error) ERR syntax error
127.0.0.1:6379> zadd list 3 4 6 7 3 2 11
(error) ERR syntax error
127.0.0.1:6379> zadd list 3 4 6 7 9 2 11
(error) ERR syntax error
127.0.0.1:6379> zadd list2 2 4 5 6 12 4
(integer) 2
127.0.0.1:6379> sort list2
1) "4"
2) "6"
127.0.0.1:6379> lpush list3 a c f e s h i o
(integer) 8
127.0.0.1:6379> sort list3 alpha   #按字段顺序排序
1) "a"
2) "c"
3) "e"
4) "f"
5) "h"
6) "i"
7) "o"
8) "s"
127.0.0.1:6379> sort list alpha desc  
(empty list or set)
127.0.0.1:6379> sort list alpha
(empty list or set)
127.0.0.1:6379> sort list3 alpha desc  #字典倒序
1) "s"
2) "o"
3) "i"
4) "h"
5) "f"
6) "e"
7) "c"
8) "a"
127.0.0.1:6379> sort list3 alpha desc limit 2 3 #从第三个元素开始取三个
1) "i"
2) "h"
3) "f"
#根据散列中的一个属性排序
127.0.0.1:6379> hset post:1 time 23
(integer) 1
127.0.0.1:6379> hset post:2 time 29
(integer) 1
127.0.0.1:6379> hset post:3 time 25
(integer) 1
127.0.0.1:6379> hset post:4 time 40
(integer) 1
127.0.0.1:6379> sadd post:ids 1 2 3 4
(integer) 4
127.0.0.1:6379> sort post:ids by post:*->time desc
1) "4"
2) "2"
3) "3"
4) "1"
#根据一个字符类型的键进行排序
127.0.0.1:6379> lpush list4 2 1 3
(integer) 3
127.0.0.1:6379> set itemscore:1 50
OK
127.0.0.1:6379> set itemscore:2 100
OK
127.0.0.1:6379> set itemscore:3 -10
OK
127.0.0.1:6379> sort list4 by itemscore:* desc
1) "2"
2) "1"
3) "3"
127.0.0.1:6379> 
转换成散列中的一个字段
127.0.0.1:6379> hmset posts:articles:1 time 23 title Java
OK
127.0.0.1:6379> hmset posts:articles:2 time 40 title Oracle
OK
127.0.0.1:6379> hmset posts:articles:3 time 11 title Scala
OK
127.0.0.1:6379> hmset posts:articles:3 time 29 title MySQL
OK
127.0.0.1:6379> lpush tags:time:ids 1 2 3 4
(integer) 4
127.0.0.1:6379> sort tart:time:ids by posts:articles:*->time get posts:articles:*->title
(empty list or set)
127.0.0.1:6379> sort tart:time:ids by posts:articles:*->time
(empty list or set)
127.0.0.1:6379> sort targ:time:ids by posts:articles:*->time
(empty list or set)
127.0.0.1:6379> sort tags:time:ids by posts:articles:*->time
1) "4"
2) "1"
3) "3"
4) "2"
127.0.0.1:6379> sort tags:time:ids by posts:articles:*->time get posts:articles:*->title
1) (nil)
2) "Java"
3) "MySQL"
4) "Oracle"
#转换成一个字符串键
127.0.0.1:6379> set title:1 aaaa
OK
127.0.0.1:6379> set title:2 bbbb
OK
127.0.0.1:6379> set title:3 cccc
OK
127.0.0.1:6379> set title:4 dddd
OK
127.0.0.1:6379> sort tags:time:ids by posts:articles:*->time get title:*
1) "dddd"
2) "aaaa"
3) "cccc"
4) "bbbb"
127.0.0.1:6379> 

“`

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值