本篇文章介绍下redis排序命令.redis支持对list,set和sorted set元素的排序。排序命令是sort
完整的命令格式如下:
SORT key [BY pattern] [LIMIT start count] [GET pattern]
[ASC|DESC] [ALPHA] [STORE
dstkey]
下面我们一一说明各种命令选项
(1)sort
key
这个是最简单的情况,没有任何选项就是简单的对集合自身元素排序并返回排序结果.下面给个例子
redis> lpush ml 12
(integer) 1
redis> lpush ml 11
(integer) 2
redis> lpush ml 23
(integer) 3
redis> lpush ml 13
(integer) 4
redis> sort ml
1. "11"
2. "12"
3. "13"
4. "23"
(2)[ASC|DESC]
[ALPHA]
sort默认的排序方式(asc)是从小到大排的,当然也可以按照逆序或者按字符顺序排。逆序可以加上desc选项,想按字母顺序排可以加alpha选项,当然alpha可以和desc一起用。下面是个按字母顺序排的例子
redis> lpush mylist baidu(3)[BY pattern]
(integer) 1
redis> lpush mylist hello
(integer) 2
redis> lpush mylist xhan
(integer) 3
redis> lpush mylist soso
(integer) 4
redis> sort mylist
1. "soso"
2. "xhan"
3. "hello"
4. "baidu"
redis> sort mylist alpha
1. "baidu"
2. "hello"
3. "soso"
4. "xhan"
redis> sort mylist desc alpha
1. "xhan"
2. "soso"
3. "hello"
4. "baidu"
redis> set name11 nihao
OK
redis> set name12 wo
OK
redis> set name13 shi
OK
redis> set name23 lala
OK
redis> sort ml by name*
1. "13"
2. "23"
3. "11"
4. "12"
redis> sort ml by name* get name* alpha这 次返回的就不在是ml中的元素了,而是name12 name13 name23 name23对应的值。当然排序是按照name12 name13 name23 name23值并根据字母顺序排的。另外get选项可以有多个。看例子(#特殊符号引用的是原始集合也就是ml)
1. "lala"
2. "nihao"
3. "shi"
4. "wo"
redis> sort ml by name* get name* get # alpha最后在还有一个引用hash类型字段的特殊字符->,下面是例子
1. "lala"
2. "23"
3. "nihao"
4. "11"
5. "shi"
6. "13"
7. "wo"
8. "12"
redis> hset user1 name hanjie很容易理解,注意当对应的user23不存在时候返回的是nil
(integer) 1
redis> hset user11 name hanjie
(integer) 1
redis> hset user12 name 86
(integer) 1
redis> hset user13 name lxl
(integer) 1
redis> sort ml get user*->name
1. "hanjie"
2. "86"
3. "lxl"
4. (nil)
redis> sort ml get name* limit 1 2start下标是从0开始的,这里的limit选项意思是从第二个元素开始获取2个
1. "wo"
2. "shi"
redis> sort ml get name* limit 1 2 store cl这个例子我们将排序结果保存到了cl中
(integer) 2
redis> type cl
list
redis> lrange cl 0 -1
1. "wo"
2. "shi"
redis> sadd tom:friend:list 123 #tom的好友列表 里面是好友的uid
1
redis> sadd tom:friend:list 456
1
redis> sadd tom:friend:list 789
1
redis> sadd tom:friend:list 101
1
redis> set uid:sort:123 1000 #uid对应的成绩
OK
redis> set uid:sort:456 6000
OK
redis> set uid:sort:789 100
OK
redis> set uid:sort:101 5999
OK
redis> set uid:123 "{‘uid‘:123,‘name‘:‘lucy‘}" #增加uid对应好友信息
OK
redis> set uid:456 "{‘uid‘:456,‘name‘:‘jack‘}"
OK
redis> set uid:789 "{‘uid‘:789,‘name‘:‘marry‘}"
OK
redis> set uid:101 "{‘uid‘:101,‘name‘:‘icej‘}"
OK
redis> sort tom:friend:list by uid:sort:* get uid:* #从好友列表中获得id与uid:sort字段匹配后排序,并根据排序后的顺序,用key在uid表获得信息
1. {‘uid‘:789,‘name‘:‘marry‘}
2. {‘uid‘:123,‘name‘:‘lucy‘}
3. {‘uid‘:101,‘name‘:‘icej‘}
4. {‘uid‘:456,‘name‘:‘jack‘}
redis> sort tom:friend:list by uid:sort:* get uid:* get uid:sort:*
1. {‘uid‘:789,‘name‘:‘marry‘}
2. 100
3. {‘uid‘:123,‘name‘:‘lucy‘}
4. 1000
5. {‘uid‘:101,‘name‘:‘icej‘}
6. 5999
7. {‘uid‘:456,‘name‘:‘jack‘}
8. 6000
if (alpha) {
if (sortby) vector[j].u.cmpobj = getDecodedObject(byval);
} else {
if (byval->encoding == REDIS_ENCODING_RAW) {
vector[j].u.score = strtod(byval->ptr,NULL);
} else if (byval->encoding == REDIS_ENCODING_INT) {
/* Don‘t need to decode the object if it‘s
* integer-encoded (the only encoding supported) so
* far. We can just cast it */
vector[j].u.score = (long)byval->ptr;
} else {
redisAssert(1 != 1);
}
}
转 http://www.cnblogs.com/redcreen/archive/2011/02/15/1955226.html
原文地址:http://www.cnblogs.com/dayhand/p/3756673.html