redis sorted set order by multi scores

2,995 views
Skip to first unread message

Liu Wei

unread,
Mar 1, 2014, 8:20:26 AM3/1/14
to redi...@googlegroups.com
I'm a new guy for redis. and now I've a problem on implement  game rank function. This game rank function as follows:
1. Winner won the game by more score and less time. 
2. Loser can challenge No.1 winner, If loser win, loser is No.1.

For now, I see I should use:
redis > zadd 'gameId'  'scores'  'player'  to order game rank, but also think about 'time'. 

Question:
1. How can I implement order by score and time. not only by score?
2. The challenge how to implement for most safe?

thanks

Josiah Carlson

unread,
Mar 2, 2014, 1:28:12 AM3/2/14
to redi...@googlegroups.com
It is not clear what you mean by "score and time, not only by score", because it's not clear how you actually compare people who have not played each other.

Can you show code in your programming language of choice, using your programming language's native data structures, how you would represent the scores and rankings?

 - Josiah


--
You received this message because you are subscribed to the Google Groups "Redis DB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to redis-db+u...@googlegroups.com.
To post to this group, send email to redi...@googlegroups.com.
Visit this group at http://groups.google.com/group/redis-db.
For more options, visit https://groups.google.com/groups/opt_out.

Liu Wei

unread,
Mar 2, 2014, 5:12:16 AM3/2/14
to redi...@googlegroups.com
Thanks Josiah,

sorry for not explain clear.

I mean I will sort user rank by more correct answer(Score) and less game time(Time). for example:

|   Rank   |     Score   |    Time   |
|    #1      |       10      |      48s   |
|    #2      |       8        |      48s   |
|    #3      |       8        |      52s   |
|    #4      |       6        |      32s   |
|    #5      |       5        |      50s   |

the above table shows:
1. #1 win #2, because he get the more score at same time;
2. #2 win #3, because he use less time for the same score;
3. #3 win #4, because even if #4 use less time, but #3 get more score.

I want to implement this table rank, not only by score rank, but also by time rank

Thanks,
Regards,
Wei

Josiah Carlson

unread,
Mar 2, 2014, 11:28:35 AM3/2/14
to redi...@googlegroups.com
You can use a variant of what I describe here: http://www.dr-josiah.com/2013/10/multi-column-sql-like-sorting-in-redis.html to build your scores. I used it to build sorting options over ZSETs with the columns split up, but you can combine them in advance if this is the only sort you care about.

Basically, what it does is to create a composite ZSET score that represents *both* your game score and your game time, at the same time. As an example, we'll pretend that your game scores range from 0 to 100 (this is necessary because scores are inversely ordered with respect to who is #1 vs. #2, etc.), and game times can range from 0 to 1000 seconds.

You can create your new score for the ZSET by:
zset_score = (100 - score) * 1000 + time

If you take examples from your list: (10, 48), (8, 48), ..., you get the ZSET scores: 90048, 92048, 92052, 94032, 95050, which will have indexes in your ZSET of 0, 1, 2, 3, and 4, respectively. Which is exactly the ranking that you'd like.

 - Josiah

Liu Wei

unread,
Mar 5, 2014, 9:44:51 AM3/5/14
to redi...@googlegroups.com
Thank you very much Josiah. It can work well now.


--
You received this message because you are subscribed to a topic in the Google Groups "Redis DB" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/redis-db/-Kzt-Fr35zk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to redis-db+u...@googlegroups.com.

To post to this group, send email to redi...@googlegroups.com.
Visit this group at http://groups.google.com/group/redis-db.
For more options, visit https://groups.google.com/groups/opt_out.



--
Best Regards,

Eric.Liu

Joel Albert

unread,
Aug 4, 2014, 3:58:14 PM8/4/14
to redi...@googlegroups.com
Sorry to NECRO a post, but I've been trying to figure out an easier way to mimic multi sorting in Redis with little to no luck. This patch seemed like a huge step in the right direction, but it seems like no one has actually put it into redis 2.8 and its been out for about 2 years. (https://github.com/antirez/redis/issues/223).  Seems odd that this functionality isn't more automated yet with Redis as im sure this use case comes up very frequently.  Has anyone else found an easier way to multisort and care to give some examples.  Thanks!

Josiah Carlson

unread,
Aug 4, 2014, 4:54:07 PM8/4/14
to redi...@googlegroups.com
The patch you are referring to uses the "SORT" command to handle sorting, but the thread here was talking about using multiple ZSETs to contribute to a sort order. So similar topics, just different structures.

The discussion over the particular patch (and #225) that you linked seems ongoing as recently as today, and seems likely to make it in at some point soon (assuming the patch can be modified to apply).


In terms of multiple column/index sorting, indeed there does exist a solution, and I linked in my earlier reply to the thread: http://www.dr-josiah.com/2013/10/multi-column-sql-like-sorting-in-redis.html

 - Josiah



For more options, visit https://groups.google.com/d/optout.

Joel Albert

unread,
Aug 4, 2014, 5:45:46 PM8/4/14
to redi...@googlegroups.com
Thanks josh, and yes I've seen your post.  I was referring to any other methods on top of this one.  Basically wondering if people have figured out other ways to mimic multi sort SQL functionality. 

Josiah Carlson

unread,
Aug 4, 2014, 7:04:40 PM8/4/14
to redi...@googlegroups.com
Aside from the method I outlined and the patch you linked to, the only other remaining methods require copying/sorting in Lua (scripted inside Redis) or somewhere else. In other words, those other methods are not viable.

 - Josiah

Joel Albert

unread,
Aug 4, 2014, 8:17:02 PM8/4/14
to redi...@googlegroups.com
Thanks again Josiah.  I'll look into possibly Lua.  

陈洋

unread,
Oct 27, 2021, 4:23:31 AM10/27/21
to Redis DB

You guys can try this: https://github.com/alibaba/TairZset which supports multi-score(Limited to 255)sorting, and the accuracy of any dimension is not lost, incrby semantics is still supported under multi-score sorting and the syntax is similar to redis zset.
Reply all
Reply to author
Forward
0 new messages