本文内容为 http://www.redis.io/commands/sort 的翻译、注解、例子扩充及其它修改。感谢 Redis 作者 Antirez 为开源社区作出的贡献,本文保证最新、最准、最全以表示对其的敬意。欢迎留言纠错、提示更新或支持。
SORT key [BY pattern] [LIMIT offset count] [GET pattern [GET pattern ...]] [ASC|DESC] [ALPHA] [STORE destination]

返回或存储保存在键 key 所对应的列表(List)、集合(Set)或有序集合(Sorted Set)中的元素。默认地,进行数值(Numeric)排序,各个元素通过被解析为双精度浮点数(Double precision floating point numbe)的值进行比较。

Returns or stores the elements contained in the list, set or sorted set at key. By default, sorting is numeric and elements are compared by their value interpreted as double precision floating point number.

这就是 SORT 命令最简单的形式:

SORT mylist

假设 mylist 是一个由数值组成的列表,当前命令将会返回元素按从小到大排序的同一列表。如果想从大到小排序,使用修改器 DESC

SORT mylist DESC

mylist 包含字符串值(String values),且你希望按字典序(Lexicographically)排序它们,使用修改器 ALPHA

SORT mylist ALPHA

Redis 基于 UTF-8 字符集,假使你已经正确的设置 !LC_COLLATE 环境变量。

Redis is UTF-8 aware, assuming you correctly set the !LC_COLLATE environment variable.

可以通过使用修改器 LIMIT 来限制返回的元素个数。该修改器使用参数 offset 来指明需要跳过的元素个数,使用参数 count 来指明从偏移 offset 开始返回元素的个数。

The number of returned elements can be limited using the LIMIT modifier. This modifier takes the offset argument, specifying the number of elements to skip and the count argument, specifying the number of elements to return from starting at offset.

下述例子将会从 0 开始(偏移 offset 是基于 0(Zero-based))返回已排序版本 mylist 中的 10 个元素。

SORT mylist LIMIT 0 10

基本上所有的修改器都能同时使用。下述例子将会返回以字典序倒序排序后的前 5 个元素:

SORT mylist LIMIT 0 5 ALPHA DESC
  • 由已排序元素组成的列表。

    List of sorted elements.
  • Sorting by external keys

    Sometimes you want to sort elements using external keys as weights to compare instead of comparing the actual elements in the list, set or sorted set. Let's say the list mylist contains the elements 1, 2 and 3 representing unique IDs of objects stored in object_1, object_2 and object_3. When these objects have associated weights stored in weight_1, weight_2 and weight_3, SORT can be instructed to use these weights to sort mylist with the following statement:

    SORT mylist BY weight_*

    The BY option takes a pattern (equal to weight_* in this example) that is used to generate the keys that are used for sorting. These key names are obtained substituting the first occurrence of * with the actual value of the element in the list (1, 2 and 3 in this example).

  • Skip sorting the elements

    The BY option can also take a non-existent key, which causes SORT to skip the sorting operation. This is useful if you want to retrieve external keys (see the GET option below) without the overhead of sorting.

    SORT mylist BY nosort
  • Retrieving external keys

    Our previous example returns just the sorted IDs. In some cases, it is more useful to get the actual objects instead of their IDs (object_1, object_2 and object_3). Retrieving external keys based on the elements in a list, set or sorted set can be done with the following command:

    SORT mylist BY weight_* GET object_*

    The GET option can be used multiple times in order to get more keys for every element of the original list, set or sorted set.

    It is also possible to GET the element itself using the special pattern #:

    SORT mylist BY weight_* GET object_* GET #
  • Storing the result of a SORT operation

    By default, SORT returns the sorted elements to the client. With the STORE option, the result will be stored as a list at the specified key instead of being returned to the client.

    SORT mylist BY weight_* STORE resultkey

    An interesting pattern using SORT ... STORE consists in associating an EXPIRE timeout to the resulting key so that in applications where the result of a SORT operation can be cached for some time. Other clients will use the cached list instead of calling SORT for every request. When the key will timeout, an updated version of the cache can be created by calling SORT ... STORE again.

    Note that for correctly implementing this pattern it is important to avoid multiple clients rebuilding the cache at the same time. Some kind of locking is needed here (for instance using SETNX).

  • Using hashes in BY and GET

    It is possible to use BY and GET options against hash fields with the following syntax:

    SORT mylist BY weight_*->fieldname GET object_*->fieldname

    The string -> is used to separate the key name from the hash field name. The key is substituted as documented above, and the hash stored at the resulting key is accessed to retrieve the specified hash field.

  • 版本支持

    1.0.0+

    时间复杂度(Time complexity)

    O(N+M*log(M))N 为被排序对象中元素的个数,M 为所需返回的元素个数。

    O(N+M*log(M)) where N is the number of elements in the list or set to sort, and M the number of returned elements. When the elements are not sorted, complexity is currently O(N) as there is a copy step that will be avoided in next releases.