SORT key [LIMIT offset count] [ASC | DESC] [ALPHA]
Time complexity: 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 O(N).
ACL categories: @write, @set, @sortedset, @list, @slow, @dangerous
By default, sorting is numeric and elements are compared by their value
interpreted as double precision floating point number.
SORT in its simplest form:
mylist is a list of numbers, this command will return the same list
with the elements sorted from small to large.
In order to sort the numbers from large to small, use the
SORT mylist DESC
mylist contains string values and you want to sort them
lexicographically, use the
SORT mylist ALPHA
Dragonfly is UTF-8 aware.
The number of returned elements can be limited using the
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
The following example will return 10 elements of the sorted version of
starting at element 0 (
offset is zero-based):
SORT mylist LIMIT 0 10
Almost all modifiers can be used together. The following example will return the first 5 elements, lexicographically sorted in descending order:
SORT mylist LIMIT 0 5 ALPHA DESC
Array reply: list of sorted elements.