Redis SortedSet:当两个值具有相同分数时,如何以数字顺序而不是字母顺序获取值? [英] Redis SortedSet: How to get values in numerical order rather than alphabetical order when two values has the same score?

查看:1310
本文介绍了Redis SortedSet:当两个值具有相同分数时,如何以数字顺序而不是字母顺序获取值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我正在使用redis SortedSet为每个用户实现游戏排名,但是我遇到了一个问题:

Recently, I'm using redis SortedSet to implement game rank for every user, but I've got a problem:

zadd game_rank 55 6435
zadd game_rank 55 6088561
zadd game_rank 55 608825
zrange game_rank 0 -1

结果是:

"60882561"
"608852"
"6435"

我想知道在相同分数时是否可以按数字顺序获取值:

I want to know if it's possible to get the values in numerical order when in same score:

"6435"
"608852"
"60882561"

推荐答案

当排序集的成员具有相同的分数时,将按照字典顺序对其进行排序.没有直接的方法可以使它们以不同的顺序排列AFAIK,但是您可以轻松地在客户端进行排序.另外,您可以编写一个简短的Lua脚本,在服务器上为您完成此操作.

When a sorted set's members have the same score, they are sorted lexicographically. There's no straightforward way to get them ordered differently AFAIK but you can easily do the sorting on the client's side. Alternatively, you could cook up a short Lua script that does that for you on the server.

您可以使用一种稍微有点怪异的方法来摆脱想要的东西,而不必求助于上述方法.根据您提供的示例,并假设rank和user id都是整数,则可以使用将两者结合在一起的得分,如下所示:

There is a slightly hackish way that you can get away with what you want without resorting to the approaches above. Based on the example you had provided and ssuming that both rank and user id are integers, you could use scores that are a combination of both, like so:

zadd game_rank 550006435 6435
zadd game_rank 556088561 6088561
zadd game_rank 550608825 608825

这将允许您进行范围,并且您将获得每个排名的数字"排序(即使用zrangebyscore game_rank 550000000 559999999).

This will let you do ranges and you'll get "numerical" sorting for each rank (i.e. with zrangebyscore game_rank 550000000 559999999).

这篇关于Redis SortedSet:当两个值具有相同分数时,如何以数字顺序而不是字母顺序获取值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆