damon
damon

Reputation: 269

redis sort by multi fields

It's easy to use sql to query with multi sort fields.For example:

select * from user order by score desc,name desc

with two fields sort(score,name).

how should do this in redis?

Upvotes: 2

Views: 1882

Answers (1)

Global Warrior
Global Warrior

Reputation: 5130

Use sorted set of redis which is sorted by score. You have to prepare score according to your needs.

finalScore = score*MAX_NAME_VALUE + getIntRepresentation(name) 
//MAX_NAME_VALUE is the maximum value returned by getIntRepresentation() method

and then use

 zadd myset finalScore value

and the just use

zrevrange myset 0 10

Upvotes: 2

Related Questions