# SORT

> Sort the elements of a list, set or sorted set, optionally by external keys, and return or store the result.

URL: https://docs.hotkv.com/commands/sort/

```
SORT key [BY pattern] [LIMIT offset count] [GET pattern [GET pattern ...]] [ASC | DESC] [ALPHA] [STORE destination]
```

- Group: Server
- Complexity: O(N + M log M) where N is the number of elements and M the number sorted
- HotKV Cloud: all plans
- On-Premise: no license needed

`SORT` returns the elements of a list, set or sorted set in sorted order. By
default the elements are compared as numbers. It can also weigh each
element by the value of another key (`BY`), return values from other keys
instead of the elements (`GET`), and save the result as a list (`STORE`).
Use [SORT_RO](https://docs.hotkv.com/commands/sort_ro/index.md) when you don't need `STORE`.

## Options

| Option | Effect |
| --- | --- |
| `ASC`, `DESC` | Ascending (default) or descending order. |
| `ALPHA` | Compare as byte strings instead of numbers. |
| `LIMIT offset count` | Return `count` elements starting at `offset`, after sorting. A negative `count` returns the rest; a negative `offset` counts as `0`. |
| `BY pattern` | Sort by weights looked up in other keys: the first `*` in `pattern` is replaced by the element. `weight_*` reads the string key `weight_<element>`, and `user_*->age` reads the `age` field of the hash `user_<element>`. A missing weight counts as `0` (or sorts first with `ALPHA`). A pattern without `*` skips sorting: elements keep their stored order (a sorted set's by score), reversed with `DESC`. |
| `GET pattern` | Return the value found with `pattern` for each element, instead of the element; same pattern syntax as `BY`, and `#` returns the element itself. Repeat `GET` to return several values per element. A missing value is returned as nil. |
| `STORE destination` | Save the result as a list at `destination`, replacing what was there (deleting it if the result is empty), and return the number of elements stored. Nil `GET` values are stored as empty strings. |

When numbers compare equal, elements are ordered by their own bytes, so the
result is stable from call to call.

## Return value

| Case | RESP2 | RESP3 |
| --- | --- | --- |
| Without `STORE` | Array of bulk strings (nil for a missing `GET` value) | Array of bulk strings (null for a missing `GET` value) |
| With `STORE` | Integer: elements stored | Integer |
| `key` does not exist | Empty array, or `0` with `STORE` | Same |

Errors: `WRONGTYPE` if `key` is not a list, set or sorted set;
`ERR One or more scores can't be converted into double` when sorting
numerically and an element or weight is not a number (use `ALPHA`);
`ERR value is not an integer or out of range` for a bad `LIMIT`; and
`ERR syntax error` for an unknown or incomplete option.

## Examples

```
127.0.0.1:6379> RPUSH scores 30 4 120 15
(integer) 4
127.0.0.1:6379> SORT scores
1) "4"
2) "15"
3) "30"
4) "120"
127.0.0.1:6379> SORT scores DESC LIMIT 0 2
1) "120"
2) "30"
127.0.0.1:6379> SADD team ada linus grace
(integer) 3
127.0.0.1:6379> HSET user:ada age 36
(integer) 1
127.0.0.1:6379> HSET user:linus age 54
(integer) 1
127.0.0.1:6379> HSET user:grace age 45
(integer) 1
127.0.0.1:6379> SORT team BY user:*->age GET # GET user:*->age
1) "ada"
2) "36"
3) "grace"
4) "45"
5) "linus"
6) "54"
127.0.0.1:6379> SORT team ALPHA STORE team:sorted
(integer) 3
```

## Compatibility

Same options and replies as Redis 7 and Valkey 8.

## See also

[SORT_RO](https://docs.hotkv.com/commands/sort_ro/index.md), [ZRANGE](https://docs.hotkv.com/commands/zrange/index.md), [LRANGE](https://docs.hotkv.com/commands/lrange/index.md),
[COMMAND GETKEYS](https://docs.hotkv.com/commands/command-getkeys/index.md)