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
請問各位:這樣的命令該如何寫,或者有沒有比較好的解決方案
你可以考慮下sorted set,set只是集合,本身是無排序的,而sorted set是支援排序的,根據score
依分數對使用者清單進行排序:user:* desc
維護兩個zset
依age排序: zadd sort_user_age username age
按score排序: zadd sort_user_score username score
接著把使用者的資訊以string方式保存,按照某種排序方式然後去mget.
想要排序一定要用zadd,但是你說的這個比較複雜了,實作得想想