dot Stop testing, start deploying your AI apps. See how with MIT Technology Review’s latest research.

Download now

3.7.1 Sorting

back to home

3.7.1 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 LISTs, SETs, and ZSETs according to data in the LIST/SET/ZSET data stored in STRING keys, or even data stored in HASHes. If you’re coming from a relational database background, you can think of SORT as like the order by clause in a SQL statement that can reference other rows and tables. Table 3.12 shows the SORT command definition.

Table 3.12 The SORT command definition
Command Example use and description
SORT SORT source-key [BY pattern] [LIMIT offset count] [GET pattern [GET pattern …]] [ASC|DESC] [ALPHA] [STORE dest-key] — Sorts the input LIST, SET, or ZSET according to the options provided, and returns or stores the result

Some of the more basic options with SORT include the ability to order the results in descending order rather than the default ascending order, consider items as though they were numbers, compare as though items were binary strings (the sorted order of the strings ‘110’ and ’12’ are different than the sorted order of the numbers 110 and 12), sorting by values not included in the original sequence, and even fetching values outside of the input LIST, SET, or ZSET.

You can see some examples that use SORT in listing 3.12. The first few lines of the listing show the addition of some initial data, and basic sorting (by numeric value and by string order). The remaining parts show how we can store data to be sorted by and/or fetched inside HASHes using a special syntax.

Listing 3.12 A sample interaction showing some uses of SORT
>>> conn.rpush('sort-input', 23, 15, 110, 7)
4

Start by adding some items to a LIST.

>>> conn.sort('sort-input')
['7', '15', '23', '110']

We can sort the items numerically.

>>> conn.sort('sort-input', alpha=True)
['110', '15', '23', '7']

And we can sort the items alphabetically.

>>> conn.hset('d-7', 'field', 5)
1L
>>> conn.hset('d-15', 'field', 1)
1L
>>> conn.hset('d-23', 'field', 9)
1L
>>> conn.hset('d-110', 'field', 3)
1L

We are just adding some additional data for SORTing and fetching.

>>> conn.sort('sort-input', by='d-*->field')
['15', '110', '7', '23']

We can sort our data by fields of HASHes.

>>> conn.sort('sort-input', by='d-*->field', get='d-*->field')
['1', '3', '5', '9']

And we can even fetch that data and return it instead of or in addition to our input data.

Sorting can be used to sort LISTs, but it can also sort SETs, turning the result into a LIST. In this example, we sorted numbers character by character (via the alpha keyword argument), we sorted some items based on external data, and we were even able to fetch external data to return. When combined with SET intersection, union, and difference, along with storing data externally inside HASHes, SORT is a powerful command. We’ll spend some time talking about how to combine SET operations with SORT in chapter 7.

Though SORT is the only command that can manipulate three types of data at the same time, basic Redis transactions can let you manipulate multiple data types with a series of commands without interruption.