Redis sort uses the sort command. The ability to sort the values of a list key, a collection key, or an ordered collection key.
A few simple examples:
(1) Sort the values of the list key (list of values)
127.0.0.1:6379> Rpush numbers 1 4 8 2 3 9 4
(integer) 7
127.0.0.1:6379> Lrange Numbers 0-1
1) "1"
2) "4"
3) "8"
4) "2"
5) "3"
6) "9"
7) "4"
127.0.0.1:6379> Sort Numbers
1) "1"
2) "2"
3) "3"
4) "4"
5) "4"
6) "8"
7) "9"
(2) Sort the values of the list key (character list characters)
127.0.0.1:6379> Rpush charlist A G B d s F
(integer) 6
127.0.0.1:6379> Lrange charlist 0-1
1) "A"
2) "G"
3) "B"
4) "D"
5) "S"
6) "F"
127.0.0.1:6379> Sort charlist Alpha
1) "A"
2) "B"
3) "D"
4) "F"
5) "G"
6) "S"
(3) Sort the values of the list key (list of strings)
127.0.0.1:6379> lpush stringlist "abc" "SDF" "123sdf" "SSSFADF"
(integer) 4
127.0.0.1:6379> Lrange stringlist 0-1
1) "SSSFADF"
2) "123SDF"
3) "SDF"
4) "ABC"
127.0.0.1:6379> Sort Stringlist Alpha
1) "123SDF"
2) "ABC"
3) "SDF"
4) "SSSFADF"
(4) Sort the collection key (character set)
127.0.0.1:6379> Sadd CharSet A F S r c b
(integer) 6
127.0.0.1:6379> Smembers CharSet
1) "C"
2) "S"
3) "R"
4) "B"
5) "A"
6) "F"
127.0.0.1:6379> Sort CharSet Alpha
1) "A"
2) "B"
3) "C"
4) "F"
5) "R"
6) "S"
127.0.0.1:6379>
(5) by option. Use the by option of the sort command to Jack_number, Peter_number, tom_number three key values as weights, sort the "Jack", "Peter", "Tom" three members in the ordered collection Test_result.
127.0.0.1:6379> Zadd Test-result 3.0 Jack 3.5 Peter 4.0 Tom
(integer) 3
127.0.0.1:6379> Zrange Test-result 0-1
1) "Jack"
2) "Peter"
3) "Tom"
127.0.0.1:6379> mset peter_number 1 tom_number 2 Jack_number 3
Ok
127.0.0.1:6379> Sort Test-result by *_number
1) "Peter"
2) "Tom"
3) "Jack"
127.0.0.1:6379>
The following descriptions include the Sort command and its options (ASC, DESC, ALPHA, LIMIT, STORE, by, GET) principles.
1. SORT <key>
Function: The key that contains the numeric key can be sorted primarily.
Example: Sort numbers
Principle: Take the list as an example, the target list numbers
1) Create an array of the same length as the numbers list. Each node structure in the array has two properties (pointer, floating point).
2) Iterate through the array, point each array item pointer to numbers each node, establish a one-to-one relationship.
3) iterate through the array and convert the list item pointed to by each pointer to a value of type double stored in the array item.
4) sort the array by floating-point values.
5) Iterate through the array starting at 0 bits of the array and return the value of the list item that the pointer refers to to the client.
Note: The sort command creates an array key of the same length for the sort key.
The data structure used by array items is: Redissortobject.
Depending on the sort option, you can use Redissortobject in different ways.
2. Alpha option
Function: With this option, the sort command can sort the containing string worth key.
Example: Sort fruits alpha
Principle: Take the Set as an example, the target set fruits.
1) Create an array of redissortobject structures with the same length as the fruits length.
2) iterate through the array and point the array pointers to the individual elements in the fruits collection.
3) Sorts the array based on the string elements in the collection.
4) Iterate through the array starting at 0 bits of the array and return the value of the list item that the pointer refers to to the client.
Note: Using this option does not use floating-point properties in Redissortobject.
3, ASC, desc options
Effect: Arranges the sorted result set in ascending (ASC) or descending (DESC) order. Key to a key that contains numeric keys.
Example: Sort numbers asc/desc
Principle: Basically the same as the sort <key> principle
Note: Use a quick row.
In ascending order, the contrast function is used to produce ascending comparison results.
In descending order, the comparison function is used to produce a descending comparison result.
4. By option
Role: The sort command can make certain string keys, or some of the fields contained by a hash key, to be used as weights for the elements and to sort a key.
By default, the sort command uses the elements contained by the sort key as the weight of the sort, and the elements themselves determine where the elements are located after they are sorted.
Example: SORT <key> by <pattern>
Principle: Take the list as an example, the target list fruits.
1) Create an array of redissortobject structures with the same length as the fruits length.
2) iterate through the array and point the array pointers to the individual elements in the fruits list.
3) Iterate through the array, looking for the corresponding weight key based on the list elements that each array item pointer refers to, and the mode (*-price) that is set by the by option.
4) Save the values of each weight key in the array floating-point property.
5) The array is sorted according to the weight value of the arrays.
6) Iterate through the array starting at 0 bits of the array and return the value of the list item that the pointer refers to to the client.
5. By option with Alpha
Function: The simple by option defaults to the assumption that the weight key holds the value as a number. If the value is a string, alpha mates are required.
Example: SORT <key> by <pattern> ALPHA
Principle: The principle is basically the same as by option. The difference is in the fifth step, which is to sort the string or the value.
6. Limit option
Function: Returns the sort command as part of a sorted result set, similar to a paged lookup.
Example: Sort alphabet Alpha limit 0 4, sort alphabet Alpha limit 2 4
Principle: The principle is basically the same as other sort commands, except that the result set size is limited by the two parameters (limit <offset count>) of limit when the data is returned to the client.
7. Get Options
Function: Sort after the keys are sorted, the values of some keys are found and returned based on the ordered elements, as well as the pattern developed by the GET option.
Example: Sort students ALPHA get *-name (sort <KEY> OPTIONS GET <PATTERN>)
Principle:
1) Create an array of redissortobject structures with the same length as the students collection.
2) iterate through the array and point the array pointers to the individual elements in the students collection.
3) Sort operations according to the parameters before the Get option.
4) Iterate through the array, looking for the corresponding key based on the collection element that the pointer element points to, and the mode given by the GET option.
5) Iterate over the key returned before the lookup, and return the value.
Note: Multiple get can exist at the same time by traversing the sorted result set, and multiple get gets the key value sequentially and returns it to the client.
Experiment: Experiment 7.1
8. Store options
Effect: Stores the result set sorted by the sort option in another key.
Example: SORT numberset STORE Temp
Principle: Put the result set into the list after sorting.
Note: Only in the list, if the target type is not a list, Redis makes data structure adjustments (previous data cleanup).
9. Option Execution Order
The sort command executes the steps:
1) Sort by: Alpah,asc/desc,by
2) Limit result set: Limit option
3) Get External key: get
4) Save sorted result set: Store
5) Return client results
Redis performs the sort command as described above, so it is essentially unrelated to the order of option visits, except for multiple get options.
Note: The order in which the get options are placed determines the structural consistency of the returned client result set.
Appendix:
Experiment 7.1
127.0.0.1:6379> sadd students "Peter" "Jack" "Tom"
(integer) 3
127.0.0.1:6379> smembers Students
1) "Jack"
2) "Peter"
3) "Tom"
127.0.0.1:6379> mset Peter-name "Peter White" Jack-name "Jack Snow" Tom-name "Tom Smith"
Ok
127.0.0.1:6379> sort students Alpha get *-name
1) "Jack Snow"
2) "Peter white"
3) "Tom Smith"
127.0.0.1:6379> mset Peter-birth "2016/3/15 22:53:01" Jack-birth "2016/3/13 22:53:12" Tom-birth "2016/3/16 22:53:15"
Ok
127.0.0.1:6379> sort students Alpha get *-name get *-birth
1) "Jack Snow"
2) "2016/3/13 22:53:12"
3) "Peter white"
4) "2016/3/15 22:53:01"
5) "Tom Smith"
6) "2016/3/16 22:53:15"
127.0.0.1:6379>
Experiment 8.1
127.0.0.1:6379> Lrange charlist 0-1
1) "A"
2) "G"
3) "B"
4) "D"
5) "S"
6) "F"
127.0.0.1:6379> del Temp
(integer) 1
127.0.0.1:6379> Sadd Temp "ASDFFASDF"
(integer) 1
127.0.0.1:6379> Smembers Temp
1) "ASDFFASDF"
127.0.0.1:6379> sort charlist Alpha Store Temp
(integer) 6
127.0.0.1:6379> type charlist
List
127.0.0.1:6379> Type Temp
List
127.0.0.1:6379> Sort CharSet Alpha Store temp
(integer) 6
127.0.0.1:6379> Type CharSet
Set
127.0.0.1:6379> Smembers Temp
(Error) Wrongtype operation against a key holding the wrong kind of value
127.0.0.1:6379> Lrange Temp 0-1
1) "A"
2) "B"
3) "C"
4) "F"
5) "R"
6) "S"
127.0.0.1:6379>
Redis Sort Theory