Redis Sort Command Usage

Source: Internet
Author: User

1. Command description
Returns or saves the sorted elements in the given list, collection, and ordered collection key.
Sorting defaults to numbers as objects, values are interpreted as double-precision floating-point numbers, and then compared.

2. General sort Usage
The simplest sort method to use is sort key and sort key desc.
Sort key: Returns the result of the key value from small to large.
Sort Key desc: Returns the result of the order of the key values from large to small.

Assuming the price list holds today's item prices, you can sort it by using the sort command:
# Cost Amount List
Redis> Lpush Price 30 1.5 10 8
(integer) 4

# sort
Redis> Sort Price
1) "1.5"
2) "8"
3) "10"
4) "30"

# Sort in reverse order
Redis 127.0.0.1:6379> Sort Price desc
1) "30"
2) "10"
3) "8"
4) "1.5"

3. Use alpha modifier to sort strings
Because the sort command has the default sort object as a number, you need to explicitly add an alpha modifier after the sort command when you need to sort the string.

# URL
redis> Lpush Website "www.ceddit.com"
(integer) 1

redis> Lpush Website "www.hlashdot.com"
(integer) 2

redis> Lpush Website "www.bnfoq.com"
(integer) 3

# Sort By default (by number)
Redis 127.0.0.1:8888> Sort website
1) "Www.bnfoq.com"
2) "Www.hlashdot.com"
3) "Www.ceddit.com"

# Sort by character
Redis 127.0.0.1:8888> Sort website Alpha
1) "Www.bnfoq.com"
2) "Www.ceddit.com"
3) "Www.hlashdot.com"

Redis 127.0.0.1:8888> Sort website Alpha desc
1) "Www.hlashdot.com"
2) "Www.ceddit.com"
3) "Www.bnfoq.com"

4. Use the limit modifier to restrict return results
The number of returned elements after sorting can be limited by the limit modifier, which accepts offset and count two parameters.
Offset: Specifies the number of elements to skip, which is the starting position.
Count: Specifies how many objects to return after you skip the specified element of offset.

The following example returns the first 5 objects of the sorted result (offset = 0 indicates that no element has been skipped).
# Add test data, List value is 1~10
Redis 127.0.0.1:6379> Rpush Age 1 3 5 7 9
(integer) 5

Redis 127.0.0.1:6379> Rpush age 2 4 6 8 10
(integer) 10

# returns the smallest 5 values in a list
Redis 127.0.0.1:6379> Sort age Limit 0 5
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"

5. Use external key to sort
You can sort by using the data from the external key as a weight instead of the default direct contrast key value.

Suppose you now have user data as follows:

The following code enters data into Redis:
# admin
Redis 127.0.0.1:6379> Lpush UID 1
(integer) 1

Redis 127.0.0.1:6379> Set User_name_1 admin
Ok

Redis 127.0.0.1:6379> Set User_level_1 9999
Ok

# Jack
Redis 127.0.0.1:6379> Lpush UID 2
(integer) 2

Redis 127.0.0.1:6379> Set user_name_2 jack
Ok

Redis 127.0.0.1:6379> Set user_level_2 10
Ok

# peter
Redis 127.0.0.1:6379> Lpush UID 3
(integer) 3

Redis 127.0.0.1:6379> Set User_name_3 Peter
Ok

Redis 127.0.0.1:6379> Set User_level_3 25
Ok

# Mary
Redis 127.0.0.1:6379> Lpush UID 4
(integer) 4

Redis 127.0.0.1:6379> set User_name_4 Mary
Ok

Redis 127.0.0.1:6379> Set User_level_4 70
Ok

6. By option
By default, the sort uid is sorted directly by the value in the UID:

Redis 127.0.0.1:6379> Sort UID
1) "1" # Admin
2) "2" # Jack
3) "3" # peter
4) "4" # Mary
By using the By option, you can have the UID sorted by the elements of other keys.

For example, the following code allows the UID key to be sorted by the size of User_level_{uid}:
Redis 127.0.0.1:6379> sort uid by user_level_*
1) "2" # jack, level = 10
2) "3" # Peter, level = 25
3) "4" # Mary, Level = 70
4) "1" # admin, Level = 9999
User_level_* is a placeholder that takes out the value in the UID and then uses that value to find the corresponding key.

For example, when the UID list is sorted, the program takes the values of the UID 1, 2, 3, 4, and then uses the values of User_level_1, user_level_2, User_level_3, and user_level_4 as weights for the sort uid.

7. Get Options
Using the Get option, you can remove the corresponding key value based on the result of the order.

For example, the following code first sorts the UID and then takes out the value of the key User_name_{uid}:

Redis 127.0.0.1:6379> sort uid Get user_name_*
1) "Admin"
2) "Jack"
3) "Peter"
4) "Mary"
The sorting results are now much more intuitive than using only the sort UID by user_level_*.

8. Get multiple external keys
You can use multiple get options at the same time to obtain values for multiple foreign keys.

The following code obtains USER_LEVEL_{UID} and User_name_{uid} by UID, respectively:
Redis 127.0.0.1:6379> sort uid get user_level_* get user_name_*
1) "9999" # Level
2) "Admin" # Name
3) "10"
4) "Jack"
5) "25"
6) "Peter"
7) "70"
8) "Mary"

Get has an additional parameter rule that can be used to get the value of the sorted key.

The following code returns the value of the UID, and its corresponding user_level_* and user_name_*, as the result:
Redis 127.0.0.1:6379> sort uid Get # get user_level_* get user_name_*
1) "1" # UID
2) "9999" # Level
3) "Admin" # Name
4) "2"
5) "10"
6) "Jack"
7) "3"
8) "25"
9) "Peter"
10) "4"
11) "70"
"Mary"

9. Get foreign keys, but not sort
By passing a non-existent key as an argument to the by option, you can let sort skip the sort operation and return the result directly:
Redis 127.0.0.1:6379> sort uid by Not-exists-key
1) "4"
2) "3"
3) "2"
4) "1"
When used alone, this usage is of little practical use.

However, by combining this usage with the GET option, you can get multiple foreign keys without sorting, which is equivalent to performing a consolidated fetch operation (similar to the JOIN keyword for SQL database).

The following code demonstrates how to get multiple foreign keys using sort, by, and get without causing a sort:
Redis 127.0.0.1:6379> sort uid by Not-exists-key get # get user_level_* get user_name_*
1) "4" # ID
2) "All" # level
3) "Mary" # Name
4) "3"
5) "25"
6) "Peter"
7) "2"
8) "10"
9) "Jack"
10) "1"
11) "9999"
"Admin"

10. Use a hash table as a get or by parameter

In addition to string keys, a hash table can also be used as a parameter to a GET or by option.
For example, for the User information table given earlier:

Instead of saving the user's name and level in the User_name_{uid} and User_level_{uid} Two string keys, we can save the user's name and level information with a hash table User_info_{uid} with the Name field and Levels field:
Redis 127.0.0.1:6379> hmset user_info_1 name Admin level 9999
Ok

Redis 127.0.0.1:6379> hmset user_info_2 name Jack Level 10
Ok

Redis 127.0.0.1:6379> hmset user_info_3 name Peter level 25
Ok

Redis 127.0.0.1:6379> hmset user_info_4 name Mary Level 70
Ok

The by and get options can then be used in the Key->field format to get the values of the fields in the hash table, where key represents the Hashtable key, and field represents the domain of the Hashtable:

Redis 127.0.0.1:6379> sort uid by user_info_*->level
1) "2"
2) "3"
3) "4"
4) "1"

Redis 127.0.0.1:6379> sort uid by User_info_*->level get User_info_*->name
1) "Jack"
2) "Peter"
3) "Mary"
4) "Admin"

11. Save sorting results
By default, the sort operation simply returns the sort result without any save operation.
You can save the sorting result to a given key by specifying a key parameter for the store option.
If the specified key already exists, the original value will be overwritten by the sorted result.

# test Data
Redis 127.0.0.1:6379> Rpush numbers 1 3 5 7 9
(integer) 5

Redis 127.0.0.1:6379> Rpush numbers 2 4 6 8 10
(integer) 10

Redis 127.0.0.1:6379> lrange Numbers 0-1
1) "1"
2) "3"
3) "5"
4) "7"
5) "9"
6) "2"
7) "4"
8) "6"
9) "8"
10) "10"

Redis 127.0.0.1:6379> Sort Numbers Store sorted-numbers
(integer) 10

# results after sorting
Redis 127.0.0.1:6379> lrange sorted-numbers 0-1
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
7) "7"
8) "8"
9) "9"
10) "10"

13. Return value
Does not use the store parameter, returns the sort result in the form of a list.
Using the store parameter, returns the number of elements in the sorted result.

Redis Sort Command Usage

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.