求教一个Redis有序集合score排序问题
发布于 9 天前 作者 JarvisQJ 422 次浏览 来自 问答

需求是,优先按照价格排序,价格相同再按时间优先。 所以就想到将价格和时间直接拼接,存在zset的score中,例如价格是0.123456789,时间戳为1564734388(s),score中存0.1234567891564734388,这样直接比较score就满足价格时间优先的规则。 问题是,score只能支持16位数字完美存储,而价格表示就要占10位,时间戳也需要10位,请教大家有没有好的办法?

1 回复

找到一个办法: While the same element can’t be repeated in a sorted set since every element is unique, it is possible to add multiple different elements having the same score. When multiple elements have the same score, they are ordered lexicographically (they are still ordered by score as a first key, however, locally, all the elements with the same score are relatively ordered lexicographically). 根据Redis文档,由于score相同时,将默认按照字典排序(即score对应的member值排序),所以讲时间戳拼接在member原有值之前即可。

回到顶部