Search code examples
pythonredisiteratorredis-py

How to traverse a sorted set in Redis in reverse order using zscan?


I have a sorted set in Redis with priorities starting from 0 up to 3. I would like to traverse this sorted set from highest to lowest priority using the python iterator zscan_iter. However, using zscan_iter gives me the items starting from 0. Is there a way to reverse the order? Unfortunately, reverse() only works on iterators and not on python generators.

I see two solutions:

  1. Use negative priorities (so instead of 3 use -3)
  2. Paginate through slices of keys using ZREVRANGEBYSCORE, however I would prefer to use an iterator.

Are there any other ways of doing this?


Solution

  • Scanning the Sorted Set with an iterator does not guarantee any order. Use ZREVRANGEBYSCORE for that.