Redis Sort
redis sort
CHAPTER 3
Commands in Redis
>>> conn.sort('sort-input', alpha=True)
And we can sort the
['110', '15', '23', '7']
items alphabetically.
>>> conn.hset('d-7', 'field', 5)
1L
>>> conn.hset('d-15', 'field', 1)
We are just adding some
1L
additional data for SORTing
>>> conn.hset('d-23', 'field', 9)
and fetching.
1L
>>> conn.hset('d-110', 'field', 3)
We can sort our
1L
data by fields of
>>> conn.sort('sort-input', by='d-*->field')
HASHes.
['15', '110', '7', '23']
>>> 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 LIST s, but it can also sort SETs, turning the result into a
LIST . In this example, we sorted numbers character by character (via the alpha key-
word 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 com-
mand. 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