This articleArticleRedis supports sorting list, set, and sorted set elements. The complete command format of the sort command is as follows:
Sort key [by pattern] [limit start count] [get pattern] [ASC | DESC] [Alpha] [store dstkey]
The following describes the various command options.
(1) Sort key
This is the simplest case. No option is to simply sort the elements of the set and return the sorting result. The following is an example.
Redis> lpush ml 12
(Integer) 1
Redis> lpush ml 11
(Integer) 2
Redis> lpush ml 23
(Integer) 3
Redis> lpush mL 13
(Integer) 4
Redis> sort ml
1."11"
2."12"
3."13"
4."23"
(2) [ASC | DESC] [Alpha]
The default sort sorting method (ASC) is from small to large. Of course, it can also be sorted in reverse or character order. The DESC option can be added in reverse order, and the Alpha option can be added in alphabetical order. Of course, Alpha can be used with DESC. The following is an example of sorting in alphabetical order.
Redis> lpush mylist Baidu
(Integer) 1
Redis> lpush mylist hello
(Integer) 2
Redis> lpush mylist xhan
(Integer) 3
Redis> lpush mylist Soso
(Integer) 4
Redis> sort mylist
1."Soso"
2."Xhan"
3."Hello"
4."Baidu"
Redis> sort mylist alpha
1."Baidu"
2."Hello"
3."Soso"
4."Xhan"
Redis> sort mylist DESC alpha
1."Xhan"
2."Soso"
3."Hello"
4."Baidu"
(3) [by pattern]
In addition to sorting by the value of the Set element, you can also combine the content of the Set element into a new key based on the given pattern and sort the content corresponding to the new key. The following example uses the ml set in the first example for Demonstration:
Redis> set name11 nihao
OK
Redis> set name12 wo
OK
Redis> set name13 Shi
OK
Redis> set name23 Lala
OK
Redis> sort mL by name *
1."13"
2."23"
3."11"
4."12"
* Indicates the element values in ml. Therefore, the sorting is based on the values corresponding to the four keys name12 name13 name23 name23. Of course, the elements in the sorted ml set are returned.
(For more information about sorting results, see the following FAQ)
(4) [get pattern]
The above examples are all elements in the returned ml set. We can also use the get option to obtain the value of the specified pattern as the new key. Let's look at a combination of examples.
Redis> sort mL by name * get name * alpha
1."Lala"
2."Nihao"
3."Shi"
4."Wo"
The returned result is not an element in ml, but a value corresponding to name12 name13 name23 name23. Of course, the sorting is based on the name12 name13 name23 name23 value and in alphabetical order. In addition, there can be multiple get options. Let's look at the example (# The special symbol references the original set, that is, ml)
Redis> sort mL by name * get name * Get # Alpha
1."Lala"
2."23"
3."Nihao"
4."11"
5."Shi"
6."13"
7."Wo"
8."12"
Finally, there is a special character that references the hash type field->. The following is an example.
Redis> hset user1 name hanjie
(Integer) 1
Redis> hset user11 name hanjie
(Integer) 1
Redis> hset user12 name 86
(Integer) 1
Redis> hset user13 name lxl
(Integer) 1
Redis> sort ml get user *-> name
1."Hanjie"
2."86"
3."Lxl"
4. (nil)
It is easy to understand that nil is returned when the corresponding user23 does not exist.
(5) [limit start count]
The results returned in the preceding example are all. The limit option can limit the number of returned results. Example
Redis> sort ml get name * limit 1 2
1."Wo"
2."Shi"
The START subscript starts from 0. Here, the limit option means to get 2 from the second element.
(6) [store dstkey]
If the set is frequently sorted in a fixed mode, caching the sorting results reduces CPU overhead. You can use the store option to save the sorting content to the specified key. The storage type is list.
Redis> sort ml get name * limit 1 2 Store Cl
(Integer) 2
Redis> type Cl
List
Redis> lrange Cl 0-1
1."Wo"
2."Shi"
In this example, we save the sorting result to Cl.
After the function is introduced, we will discuss some sorting issues. If we have multiple redis servers, different keys may exist on different servers. For example, name12 name13 name23 name23 may be stored on four different servers. This will have a great impact on the sorting performance. The author of redis mentioned the solution to this problem on his blog, that is, to put all the keys to be sorted on the same server through the key tag. It is generally implemented by means of client-side hash to determine which key exists on which server. We can hash the key only. For example, if our client finds that the key contains []. Then, only the [] Contents in the key are hashed. We name the keys related to four names in this way [name] 12 [name] 13 [name] 23 [name] 23, so the client Program They will all be placed on the same server. I don't know if jredis has been implemented.
Another problem is also serious. If the sort set is large, sorting takes a long time. Because redis is single-threaded, long sorting operations will block requests from other clients. The solution is to use master-slave Replication
Mechanism will Data is copied to multiple slave instances. Then we only perform sorting on slave. Cache the sorting result. Another solution is to use sorted set to create an index for the set that needs to be accessed in a certain order.
Instance:
Redis > Sadd Tom: Friend: List 123 # The Friends List of Tom contains the Friends uid.
1
Redis > Sadd Tom: Friend: List 456
1
Redis > Sadd Tom: Friend: List 789
1
Redis > Sadd Tom: Friend: List 101
1
Redis > Set UID: sort: 123 1000 # uid score
OK
Redis > Set UID: sort: 456 6000
OK
Redis > Set UID: sort: 789 100
OK
Redis > Set UID: sort: 101 5999
OK
Redis > Set UID: 123 "{'uid': 123, 'name': 'Lucy '}" # Add UID-related friend information
OK
Redis > Set UID: 456 "{'uid': 456, 'name': 'jack '}"
OK
Redis > Set UID: 789 "{'uid': 789, 'name': 'marry '}"
OK
Redis > Set UID: 101 "{'uid': 101, 'name': 'icej '}"
OK
Redis > Sort Tom: Friend: List by uid: sort: * Get uid: * # obtain the ID matching the UID: Sort field from the friends list and sort it according to the sorted order, obtain information from uid table with key
1. {'uid': 789, 'name': 'marry '}
2. {'uid': 123, 'name': 'Lucy '}
3. {'uid': 101, 'name': 'icej '}
4. {'uid': 456, 'name': 'jack '}
Redis > Sort Tom: Friend: List by uid: sort: * Get uid: sort :*
1. {'uid': 789, 'name': 'marry '}
2. 100
3. {'uid': 123, 'name': 'Lucy '}
4. 1000
5. {'uid': 101, 'name': 'icej '}
5999.
7. {'uid': 456, 'name': 'jack '}
8. 6000
FAQ:
1. Why is the sort mL by name * get name * Get # sorted by Shi Lala nihao wo? The results of this documentary pure sorting name * and name * alpha are different. This problem needs to be analyzed from the implementation logic of redis. A) after the list is inserted, it is arranged in reverse chronological order by default. The result of lrange ml 0-1 is 13 23 11 12. this is because when list is inserted, the latest item is inserted to the linked list header B) Sort m1 by name * is determined to sort by name. however, when the value corresponding to name * is not of the num type and Alpha is not set, the sorting score will be the same, because the program will convert the value corresponding to name * to nun type C), this will cause sort mL by name * to be arranged in the natural order of ML.
If (alpha) {
If (sortby) vector [J]. u. cmpobj = getdecodedobject (byval);
}< span> else {< br> If (byval-> encoding = redis_encoding_raw) {
vector [J]. u. score = strtodd (byval-> PTR, null );
}< span> else If (byval-> encoding = redis_encoding_int) {
/* don't need to decode the object if it's
* integer-e Ncoded (the only encoding supported) So
* far. we can just cast it */
vector [J]. u. score = ( long ) byval-> PTR;
}< span> else {< br> redisassert (1! = 1);
}< BR >}