redis sorted set highest score
Asked Answered
M

2

7

Is any simple method to get the highest score from Redis sorted set? I found this way, may be there is better ways to make this(in ruby):

all_scores = Redis.zrange('foo', 0, -1, with_scores: true) # => [["item 1", 2.5], ["item 2", 3.4]]
all_scores.flatten.last # => 3.4

It seems not the best way.

Marrero answered 21/6, 2016 at 11:55 Comment(0)
B
22

you can use ZREVRANGE command.

ZREVRANGE foo 0 0 withscores

This will give you the highest score and it's value.

http://redis.io/commands/zrevrange

Boggle answered 21/6, 2016 at 12:40 Comment(1)
Or get the same effect w/ ZRANGE foo -1 -1 WITHSCORESStockbreeder
G
1

For Redis 6, this is the command.

ZRANGE foo 0 0 REV
Gadid answered 9/7, 2023 at 19:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.