How do you retrieve the top ten highest scoring players from a Redis leaderboard?
Short answer
You use the ZREVRANGE command, specifying the sorted set key and a range from zero to nine, which returns the ten members with the highest scores in descending order, since ZREVRANGE returns results from highest to lowest score by default.
Advertisement
AModel answer
You use the ZREVRANGE command, specifying the sorted set key and a range from zero to nine, which returns the ten members with the highest scores in descending order, since ZREVRANGE returns results from highest to lowest score by default.
Example
ZREVRANGE leaderboard 0 9 WITHSCORES
Real-world example
A gaming leaderboard screen displays the current top ten players by calling ZREVRANGE, instantly retrieving both the player names and their scores in the correct descending order.
Likely follow-ups
- What is the difference between ZRANGE and ZREVRANGE?
- How would you retrieve the bottom ten lowest scoring players instead?
Related topics