redis排序问题,请各位不吝赐教
阿神
阿神 2017-04-21 10:57:20
0
4
559
redis localhost:6379> sadd userlist 1 
(integer) 1 
redis localhost:6379> sadd userlist 2 
(integer) 1 
redis localhost:6379> sadd userlist 3 
(integer) 1 
redis localhost:6379> sadd userlist 4 
(integer) 1

redis localhost:6379> sadd userlist 5
(integer) 1

redis localhost:6379> set score:user:1 20 
OK 
redis localhost:6379> set score:user:2 15 
OK 
redis localhost:6379> set score:user:3 11 
OK 
redis localhost:6379> set score:user:4 24 
OK

redis localhost:6379> set score:user:5 15
OK


redis localhost:6379> set age:user:1 29 
OK 
redis localhost:6379> set age:user:2 35 
OK 
redis localhost:6379> set age:user:3 25 
OK 
redis localhost:6379> set age:user:4 31

OK

redis localhost:6379> set age:user:5 27
OK

如上redis结构,如何实现sort的时候根据score倒排序,如果score相等(上面user2和user5的score相等),那么按照age正排序?

期望最后得到的userid排序结果为:

4

1

5

2

3

请问各位:这样的命令该如何写,或者有没有比较好的解决方案

阿神
阿神

闭关修行中......

reply all(4)
巴扎黑

You can consider sorted set. Set is just a collection and itself is unsorted, while sorted set supports sorting according to score

Peter_Zhu

sort userlist by score:user:* desc

刘奇

Maintain two zsets
Sort by age: zadd sort_user_age username age
Sort by score: zadd sort_user_score username score

Then save the user's information in string format, sort it in a certain way and then go to mget.

小葫芦

If you want to sort, you must use zadd, but what you said is more complicated, so you need to think about it

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template