The sorted collection associates a rank with each item in the collection.
Sorted collections work similarly to collections, and they use similar commands, except
But they are slightly different.
ZADD names 1 "Flavio" ZADD names 2 "Syd" ZADD names 2 "Roger"
As you can see, values must still be unique, but now they are associated to a score.
The score does not have to be unique.
Items in a set are always sorted by the score.
This is very useful to implement some kind of data storage tool like (usual example) a leaderboard. Or to indicate the time some item was added, with a timestamp.
You can get the score of an item using
ZRANK names "Flavio"
List all items in a sorted set using
ZRANGE, which works similarly to
ZRANGE names 0 -1
WITHSCORESto also return the scores information:
You can increment the score of an item in the set using
See all the sorted sets commands here.
More redis tutorials:
- Introduction to Redis
- How to install Redis
- First steps with Redis
- Redis Lists
- Using Redis Sets
- How to use Redis Sorted Lists
- How to use Redis Hashes
- Redis Publish/subscribe