antirez/redis — Issue #196: LREVRANGE command
Author: german
Created: 2011-11-20 19:44:16+00:00
State: open

It's not an issue but rather a question: why there's no LREVRANGE command in Redis?

As far as I know in redis lists are double linked (correct me if I'm wrong) so is there a reason why you didn't implemented it?

```sh
redis 127.0.0.1:6379> LPUSH test, 1
(integer) 1
redis 127.0.0.1:6379> LPUSH test, 3
(integer) 2
redis 127.0.0.1:6379> LPUSH test, 9
(integer) 3
redis 127.0.0.1:6379> LPUSH test, 81
(integer) 4
redis 127.0.0.1:6379> LRANGE test, 0, 3
1) "81"
2) "9"
3) "3"
4) "1"
redis 127.0.0.1:6379> LRANGE test, -4, -1
1) "81"
2) "9"
3) "3"
4) "1"
```

But what if I need to do this:

```sh
redis 127.0.0.1:6379> LREVRANGE test, 0, 3
1) "1"
2) "3"
3) "9"
4) "81"
```

I'm asking this because I'm writing sort-by-attribute functionality for my redis_orm ruby gem (it may sound bizarre but it's ORM for redis with ActiveRecord's 2.x API).

I already wrote part of it based on kind of a binary tree (https://github.com/german/redis_orm/blob/sortable/lib/redis_orm/redis_orm.rb#L244 and https://github.com/german/redis_orm/blob/sortable/lib/redis_orm/redis_orm.rb#L653) but only for ascending list order so queries like this would work:

```ruby
User.find(:all, :order => [:name, :asc])
```

But not this:

```ruby
User.find(:all, :order => [:name, :desc])
```

To achieve later redis_orm must maintain 2 lists (1 for ascending order and 1 with the same record ids for descending one) per sortable attribute or invoke a simple #revert method of all ids (which is inefficient when user wants to get only part of his records, or the number of records is large).

Could you advise please? Thanks!