Any way to use strings as scores in a Redis sorted set (zset)?

Or maybe the question should be: What is the best way to represent a string as a number, so that sorting their numeric representations will give the same result as if it were sorted as strings? I worked out a way that could sort up to 9 characters per line, but it seems like there should be a much better way.

In advance, I don't think using Redis lexicographic commands will work. (See next example.)

Example. Suppose I want to trap all names associated with some ID so that I can use ZINTERSTORE to quickly get an ordered list of IDs based on their names (without using the redis' SORT command). Ideally, I would have ID elements as zset members, and the numeric representation of each name would be a zset icon.

It makes sense? Or I'm wrong?

+3


source to share


1 answer


You are trying to use an order preserving hash function to generate a score for each ID. While it looks like you wrote one, you already figured out that the evaluation range only allows the first 9 characters (it would be interesting to see your btw function).



Instead of this approach, it is easier than simpler IMO - use formset items <name>:<id>

and set the score to 0. You can use lexicographic ordering in this way and use something like split(':')

to get an id from given items.

+1


source







All Articles