ZRANK(3valkey)
β¨ ZRANK β¨
okay bestie, welcome to the valkey command manual, page three of zrank, and honestly? this one is a whole vibe π
πΈ NAME πΈ
zrank β returns the index of a member in a sorted set ordered by ascending scores. that's it, that's the girl π
β¨ SYNOPSIS β¨
ZRANK key member [WITHSCORE]
π DESCRIPTION π
so basically, ZRANK returns the rank of member in the sorted set stored at key, with the scores ordered from low to high. and the rank (aka the index) is 0-based, meaning the member with the lowest score gets rank 0. zero. the first one. no cap π₯
the optional WITHSCORE argument supplements the command's reply with the score of the element returned. so you get the rank and the score. it's giving two-for-one, and we love that π«Ά
want the rank with scores ordered from high to low instead? that's ZREVRANK's whole job, go bother her π
π¦ REPLY π¦
RESP2
one of the following, bestie:
- valkey-protocol(7) Nil reply: if the
keydoes not exist or thememberdoes not exist in the sorted set. sad girl hours π - valkey-protocol(7) Integer reply: the rank of the
memberwhenWITHSCOREis not used. - valkey-protocol(7) Array reply: the rank and score of the
memberwhenWITHSCOREis used.
RESP3
one of the following:
- valkey-protocol(7) Null reply: if the
keydoes not exist or thememberdoes not exist in the sorted set. same energy, different name π - valkey-protocol(7) Integer reply: the rank of the
memberwhenWITHSCOREis not used. - valkey-protocol(7) Array reply: the rank and score of the
memberwhenWITHSCOREis used.
β‘ COMPLEXITY β‘
O(log(N)) β lowkey efficient, we stan π
π ACL CATEGORIES π
@fast @read @sortedset
π HISTORY π
- Available since:
2.0.0β she's been around, an icon, a legend β - Changed in
7.2.0: Added the optionalWITHSCOREargument. glow-up era π
π EXAMPLES π
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> ZRANK myzset "three"
(integer) 2
127.0.0.1:6379> ZRANK myzset "four"
(nil)
127.0.0.1:6379> ZRANK myzset "three" WITHSCORE
1) (integer) 2
2) "3"
127.0.0.1:6379> ZRANK myzset "four" WITHSCORE
(nil)
see how "three" pulls up at rank 2? main character behavior. and "four"? not invited, (nil), bye π
π 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), zrem(3valkey), zremrangebylex(3valkey), zremrangebyrank(3valkey), zremrangebyscore(3valkey), zrevrange(3valkey), zrevrangebylex(3valkey), zrevrangebyscore(3valkey), zrevrank(3valkey), zscan(3valkey), zscore(3valkey), zunion(3valkey), zunionstore(3valkey)
and that's the whole page, bestie!! this was valkey version 9.0.0, dated 2025-10-21, and you absolutely ate reading all of it. go forth and rank your sorted sets. periodt πβ¨π
π¦ where this page comes from
- fedora 44: valkey-doc 9.0.3-2.fc44 Β· CC-BY-SA-4.0
- fedora 44: valkey-doc 9.0.6-1.fc44 Β· CC-BY-SA-4.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms