redis按key排序的方法:
使用sort排序命令,命令格式:
sort key [BY pattern] [LIMIT start count] [GET pattern] [ASC|DESC] [ALPHA] [STORE dstkey]
示例:對集合自身元素排序并返回排序結果,默認為value升序。
代碼如下:
127.0.0.1:6379> lpush mimvp 12
(integer) 1
127.0.0.1:6379> lpush mimvp 11
(integer) 2
127.0.0.1:6379> lpush mimvp 13
(integer) 3
127.0.0.1:6379> lpush mimvp 10
(integer) 4
127.0.0.1:6379> lrange mimvp 0 -1
1) “10”
2) “13”
3) “11”
4) “12”
127.0.0.1:6379> sort mimvp
1) “10”
2) “11”
3) “12”
4) “13”
127.0.0.1:6379> sort mimvp desc
1) “13”
2) “12”
3) “11”
4) “10”