SORT

Since 1.1.0Keys

Syntax

SORT key [BY pattern] [LIMIT offset count] [GET pattern [...]] [ASC|DESC] [ALPHA] [STORE destination]
  • Flags: movablekeys, write
  • ACL categories: @dangerous, @generic, @keyspace, @slow, @write
  • Arity: -2

Sorts the elements of a list, set or sorted set, as numbers unless ALPHA is given.

Options

OptionEffect
ASC, DESCAscending (the default) or descending.
ALPHASort as strings, not numbers.
LIMIT offset countReturn count elements from offset.
BY patternSort by other keys: * in the pattern is replaced by each element, and a ->field suffix reads a hash field.
GET patternReturn other keys' values instead of the elements, the same way; GET # is the element itself. Can be given more than once.
STORE destinationStore the result as a list at destination instead of returning it.

Reply

  • The sorted elements, or what GET reads.
  • With STORE, the number of elements stored.
  • ERR One or more scores can't be converted into double when sorting as numbers and an element is not a number.

Examples

> RPUSH nums 3 1 2
(integer) 3
> SORT nums DESC
1) "3"
2) "2"
3) "1"
> RPUSH words b c a
(integer) 3
> SORT words ALPHA
1) "a"
2) "b"
3) "c"
> MSET w_1 30 w_2 10 w_3 20
OK
> SORT nums BY w_*
1) "2"
2) "3"
3) "1"
> HSET o_1 name one
(integer) 1
> SORT nums GET # GET o_*->name LIMIT 0 1
1) "1"
2) "one"