Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis Command To Sort Keys

The Redis command keys * will return a list of all keys and scan 0 is a more recent way to do something similar, but with less blocking. Do any commands exist that can sort the keys found, such as alphabetically, numerically, by creation timestamp, etc.?

Example of standard and fancy command sought:

standard keys post:* command:

post:2150
post:2400
post:1001
post:1006

fancy command to sort the keys alphabetically:

post:1001
post:1006
post:2150
post:2400
like image 451
Guessed Avatar asked Apr 18 '15 00:04

Guessed


People also ask

How do I sort a set in Redis?

To create a sorted set, use the zadd command. zadd accepts as arguments the name of the key that will hold the sorted set, followed by the score of the member you're adding and the value of the member itself.

Does Redis support sorting?

Sorting in Redis is similar to sorting in other languages: we want to take a sequence of items and order them according to some comparison between elements. SORT allows us to sort LIST s, SET s, and ZSET s according to data in the LIST / SET / ZSET data stored in STRING keys, or even data stored in HASH es.

How can I see all Redis keys?

To list the keys in the Redis data store, use the KEYS command followed by a specific pattern. Redis will search the keys for all the keys matching the specified pattern. In our example, we can use an asterisk (*) to match all the keys in the data store to get all the keys.

What does Redis use to sort the elements of a sorted set?

A Redis sorted set is a collection of unique strings (members) ordered by an associated score. When more than one string has the same score, the strings are ordered lexicographically. Some use cases for sorted sets include: Leaderboards.


2 Answers

You can always use your unix commands which works perfectly

redis-cli --scan --pattern yourpattern* | sort

like image 165
Guido Dizioli Avatar answered Oct 11 '22 12:10

Guido Dizioli


Redis returns keys, using KEYS (don't use it in production!) or SCAN, unordered. There is no "fancy" API to return them sorted but you can do it in the client application.

like image 42
Itamar Haber Avatar answered Oct 11 '22 13:10

Itamar Haber