ZREVRANK(3valkey)

πŸ“¦
version 9.0.6 Β· 2 versions
, in fedora 44

✨ NAME ✨

zrevrank β€” bestie, this one hands you the index of a member in a sorted set, but ordered by descending scores. Biggest score first, no cap. πŸ‘‘


🌸 SYNOPSIS 🌸

ZREVRANK key member [WITHSCORE]

πŸ’– DESCRIPTION πŸ’–

Okay so picture this: you got a sorted set chilling at key, and you wanna know where member ranks when everything's sorted from high to low. That's literally it. That's the whole vibe. πŸ’…

The rank (or index, same thing bestie) is 0-based, which means the member with the highest score gets rank 0. Zero. The top of the podium. Main character energy only. πŸ₯‡

The optional WITHSCORE argument is the glow-up: it supplements the command's reply with the score of the element returned. So you get the rank and the score, like a little combo meal. πŸ“

And if you want the rank ordered from low to high instead? That's ZRANK's job, not hers. Different girl, different energy. πŸ’œ


πŸ¦‹ REPLY πŸ¦‹

🌈 RESP2

One of the following, bestie:

  • valkey-protocol(7) Nil reply: if the key does not exist or the member does not exist in the sorted set. (aka ghosted 😭)
  • valkey-protocol(7) Integer reply: The rank of the member when WITHSCORE is not used.
  • valkey-protocol(7) Array reply: The rank and score of the member when WITHSCORE is used.

🌈 RESP3

One of the following, same energy different protocol:

  • valkey-protocol(7) Null reply: if the key does not exist or the member does not exist in the sorted set.
  • valkey-protocol(7) Integer reply: The rank of the member when WITHSCORE is not used.
  • valkey-protocol(7) Array reply: The rank and score of the member when WITHSCORE is used.

πŸ”₯ COMPLEXITY πŸ”₯

O(log(N)) β€” fast, efficient, iconic. She's not doing allat. ⚑


πŸŽ€ ACL CATEGORIES πŸŽ€

@fast @read @sortedset


πŸ’€ HISTORY πŸ’€

  • Available since: 2.0.0 (an elder, honestly, she's been here)
  • Changed in 7.2.0: Added the optional WITHSCORE argument. The glow-up era. ✨

🌸 EXAMPLES 🌸

Watch her work, bestie:

127.0.0.1:6379> ZADD myzset 1 "one"
(integer) 1
127.0.0.1:6379> ZADD myzset 2 "two"
(integer) 1
127.0.0.1:6379> ZADD myzset 3 "three"
(integer) 1
127.0.0.1:6379> ZREVRANK myzset "one"
(integer) 2
127.0.0.1:6379> ZREVRANK myzset "four"
(nil)
127.0.0.1:6379> ZREVRANK myzset "three" WITHSCORE
1) (integer) 0
2) "3"
127.0.0.1:6379> ZREVRANK myzset "four" WITHSCORE
(nil)

See how "three" with score 3 snagged rank 0? Highest score, highest rank spot. Periodt. πŸ’… And "four"? Not in the set, so she said (nil) and moved on. Not her problem. πŸ™ˆ


🫢 SEE ALSO 🫢

bzmpop(3valkey), bzpopmax(3valkey), bzpopmin(3valkey), zadd(3valkey), zcard(3valkey), zcount(3valkey), zdiff(3valkey), zdiffstore(3valkey), zincrby(3valkey), zinter(3valkey), zintercard(3valkey), zinterstore(3valkey), zlexcount(3valkey), zmpop(3valkey), zmscore(3valkey), zpopmax(3valkey), zpopmin(3valkey), zrandmember(3valkey), zrange(3valkey), zrangebylex(3valkey), zrangebyscore(3valkey), zrangestore(3valkey), zrank(3valkey), zrem(3valkey), zremrangebylex(3valkey), zremrangebyrank(3valkey), zremrangebyscore(3valkey), zrevrange(3valkey), zrevrangebylex(3valkey), zrevrangebyscore(3valkey), zscan(3valkey), zscore(3valkey), zunion(3valkey), zunionstore(3valkey)

The whole sorted-set friend group, fr fr. Go say hi. 🧚


okay that's the whole page bestie, you ate πŸ’–

This one's from Valkey version 9.0.0, dated 2025-10-21, and yeah, it's the ZREVRANK(3valkey) page β€” now go rank something. Slay. πŸ‘‘πŸŒΈ

πŸ“¦ where this page comes from