{
  "schema_version": 2,
  "id": "commands/sort",
  "title": "SORT",
  "url": "https://redis.io/docs/latest/commands/sort/",
  "summary": "Sorts the elements in a list, a set, or a sorted set, optionally storing the result.",
  "since": "1.0.0",
  "tags": [
    "docs",
    "develop",
    "stack",
    "oss",
    "rs",
    "rc",
    "oss",
    "kubernetes",
    "clients"
  ],
  "last_updated": "2026-09-18T09:48:32+01:00",
  "page_type": "content",
  "content_hash": "d7b3d9b78fc0666875bebcc3680d25497a2c532a4514cb436fcd5fb2720ae9fd",
  "sections": [
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "Returns or stores the elements contained in the [list](https://redis.io/docs/latest/develop/data-types#lists), [set](https://redis.io/docs/latest/develop/data-types#set) or\n[sorted set](https://redis.io/docs/latest/develop/data-types#sorted-sets) at `key`.\n\nThere is also the [`SORT_RO`](https://redis.io/docs/latest/commands/sort_ro) read-only variant of this command.\n\nBy default, sorting is numeric and elements are compared by their value\ninterpreted as double precision floating point number.\nThis is `SORT` in its simplest form:\n\n[code example]\n\nAssuming `mylist` is a list of numbers, this command will return the same list\nwith the elements sorted from small to large.\nIn order to sort the numbers from large to small, use the `DESC` modifier:\n\n[code example]\n\nWhen `mylist` contains string values and you want to sort them\nlexicographically, use the `ALPHA` modifier:\n\n[code example]\n\nRedis is UTF-8 aware, assuming you correctly set the `LC_COLLATE` environment\nvariable.\n\nThe number of returned elements can be limited using the `LIMIT` modifier.\nThis modifier takes the `offset` argument, specifying the number of elements to\nskip and the `count` argument, specifying the number of elements to return from\nstarting at `offset`.\nThe following example will return 10 elements of the sorted version of `mylist`,\nstarting at element 0 (`offset` is zero-based):\n\n[code example]\n\nAlmost all modifiers can be used together.\nThe following example will return the first 5 elements, lexicographically sorted\nin descending order:\n\n[code example]"
    },
    {
      "id": "required-arguments",
      "title": "Required arguments",
      "role": "content",
      "text": "<details open><summary><code>key</code></summary>\n\nThe name of the key to sort. It may hold a list, set, or sorted set.\n\n</details>"
    },
    {
      "id": "optional-arguments",
      "title": "Optional arguments",
      "role": "parameters",
      "text": "<details open><summary><code>BY pattern</code></summary>\n\nSort by the values of the external keys matched by `pattern` (each `*` is replaced by an element) instead of by the elements themselves. A pattern with no `*`, or a hash-field pattern that resolves the same for all elements, skips sorting.\n\n</details>\n\n<details open><summary><code>LIMIT offset count</code></summary>\n\nReturn a slice of the sorted result: skip `offset` elements and return up to `count`.\n\n</details>\n\n<details open><summary><code>GET pattern [GET pattern ...]</code></summary>\n\nReturn the values of the external keys matched by `pattern` for each sorted element. Use `GET #` to also return the element itself. May be given multiple times.\n\n</details>\n\n<details open><summary><code>ASC | DESC</code></summary>\n\nSort in ascending (`ASC`, the default) or descending (`DESC`) order.\n\n</details>\n\n<details open><summary><code>ALPHA</code></summary>\n\nSort the elements lexicographically instead of numerically.\n\n</details>\n\n<details open><summary><code>STORE destination</code></summary>\n\nStore the resulting list in `destination` instead of returning it.\n\n</details>"
    },
    {
      "id": "details",
      "title": "Details",
      "role": "content",
      "text": ""
    },
    {
      "id": "sorting-by-external-keys",
      "title": "Sorting by external keys",
      "role": "content",
      "text": "Sometimes you want to sort elements using external keys as weights to compare\ninstead of comparing the actual elements in the list, set or sorted set.\nLet's say the list `mylist` contains the elements `1`, `2` and `3` representing\nunique IDs of objects stored in `object_1`, `object_2` and `object_3`.\nWhen these objects have associated weights stored in `weight_1`, `weight_2` and\n`weight_3`, `SORT` can be instructed to use these weights to sort `mylist` with\nthe following statement:\n\n[code example]\n\nThe `BY` option takes a pattern (equal to `weight_*` in this example) that is\nused to generate the keys that are used for sorting.\nThese key names are obtained substituting the first occurrence of `*` with the\nactual value of the element in the list (`1`, `2` and `3` in this example)."
    },
    {
      "id": "skip-sorting-the-elements",
      "title": "Skip sorting the elements",
      "role": "content",
      "text": "The `BY` option can also take a non-existent key, which causes `SORT` to skip\nthe sorting operation.\nThis is useful if you want to retrieve external keys (see the `GET` option\nbelow) without the overhead of sorting.\n\n[code example]"
    },
    {
      "id": "retrieving-external-keys",
      "title": "Retrieving external keys",
      "role": "content",
      "text": "Our previous example returns just the sorted IDs.\nIn some cases, it is more useful to get the actual objects instead of their IDs\n(`object_1`, `object_2` and `object_3`).\nRetrieving external keys based on the elements in a list, set or sorted set can\nbe done with the following command:\n\n[code example]\n\nThe `GET` option can be used multiple times in order to get more keys for every\nelement of the original list, set or sorted set.\n\nIt is also possible to `GET` the element itself using the special pattern `#`:\n\n[code example]"
    },
    {
      "id": "restrictions-for-using-external-keys",
      "title": "Restrictions for using external keys",
      "role": "limits",
      "text": "Before 7.4, when enabling `Redis cluster-mode` there is no way to guarantee the existence of the external keys on the node which the command is processed on. In this case, any use of [`GET`](https://redis.io/docs/latest/commands/get) or `BY` which reference external key pattern will cause the command to fail with an error.\n\nStarting from 7.4, pattern with hash tag can be mapped to a slot, and so in `Redis cluster-mode`, the use of `BY` or [`GET`](https://redis.io/docs/latest/commands/get) is allowed when pattern contains hash tag and implies a specific slot which the key is also in, which means any key matching this pattern must be in the same slot as the key, and therefore in the same node. For example, in cluster mode, `{mylist}weight_*` is acceptable as a pattern when sorting `mylist`, while pattern `{abc}weight_*` will be denied, causing the command to fail with an error.\n\nTo use pattern with hash tag, see [Hash tags](https://redis.io/docs/latest/operate/oss_and_stack/reference/cluster-spec#hash-tags) for more information.\n\nStarting from Redis 7.0, any use of [`GET`](https://redis.io/docs/latest/commands/get) or `BY` which reference external key pattern will only be allowed in case the current user running the command has full key read permissions.\nFull key read permissions can be set for the user by, for example, specifying `'%R~*'` or `'~*` with the relevant command access rules.\nYou can check the [`ACL SETUSER`](https://redis.io/docs/latest/commands/acl-setuser) command manual for more information on setting ACL access rules.\nIf full key read permissions aren't set, the command will fail with an error."
    },
    {
      "id": "storing-the-result-of-a-sort-operation",
      "title": "Storing the result of a SORT operation",
      "role": "content",
      "text": "By default, `SORT` returns the sorted elements to the client.\nWith the `STORE` option, the result will be stored as a list at the specified\nkey instead of being returned to the client.\n\n[code example]\n\nAn interesting pattern using `SORT ... STORE` consists in associating an\n[`EXPIRE`](https://redis.io/docs/latest/commands/expire) timeout to the resulting key so that in applications where the result\nof a `SORT` operation can be cached for some time.\nOther clients will use the cached list instead of calling `SORT` for every\nrequest.\nWhen the key will timeout, an updated version of the cache can be created by\ncalling `SORT ... STORE` again.\n\nNote that for correctly implementing this pattern it is important to avoid\nmultiple clients rebuilding the cache at the same time.\nSome kind of locking is needed here (for instance using [`SETNX`](https://redis.io/docs/latest/commands/setnx))."
    },
    {
      "id": "using-hashes-in-by-and-get",
      "title": "Using hashes in `BY` and `GET`",
      "role": "content",
      "text": "It is possible to use `BY` and `GET` options against hash fields with the\nfollowing syntax:\n\n[code example]\n\nThe string `->` is used to separate the key name from the hash field name.\nThe key is substituted as documented above, and the hash stored at the resulting\nkey is accessed to retrieve the specified hash field."
    },
    {
      "id": "redis-software-and-redis-cloud-compatibility",
      "title": "Redis Software and Redis Cloud compatibility",
      "role": "content",
      "text": "| Redis<br />Software | Redis<br />Cloud | <span style=\"min-width: 9em; display: table-cell\">Notes</span> |\n|:----------------------|:-----------------|:------|\n| <span title=\"Supported\">&#x2705; Standard</span><br /><span title=\"Supported\"><nobr>&#x2705; Active-Active</nobr></span> | <span title=\"Supported\">&#x2705; Standard</span><br /><span title=\"Supported\"><nobr>&#x2705; Active-Active</nobr></span> |  |"
    },
    {
      "id": "return-information",
      "title": "Return information",
      "role": "returns",
      "text": "**RESP2:**\n\n[Array reply](../../develop/reference/protocol-spec#arrays): without passing the _STORE_ option, the command returns a list of sorted elements.\n[Integer reply](../../develop/reference/protocol-spec#integers): when the _STORE_ option is specified, the command returns the number of sorted elements in the destination list.\n\n**RESP3:**\n\n[Array reply](../../develop/reference/protocol-spec#arrays): without passing the _STORE_ option, the command returns a list of sorted elements.\n[Integer reply](../../develop/reference/protocol-spec#integers): when the _STORE_ option is specified, the command returns the number of sorted elements in the destination list."
    }
  ],
  "examples": [
    {
      "id": "overview-ex0",
      "language": "plaintext",
      "code": "SORT mylist",
      "section_id": "overview"
    },
    {
      "id": "overview-ex1",
      "language": "plaintext",
      "code": "SORT mylist DESC",
      "section_id": "overview"
    },
    {
      "id": "overview-ex2",
      "language": "plaintext",
      "code": "SORT mylist ALPHA",
      "section_id": "overview"
    },
    {
      "id": "overview-ex3",
      "language": "plaintext",
      "code": "SORT mylist LIMIT 0 10",
      "section_id": "overview"
    },
    {
      "id": "overview-ex4",
      "language": "plaintext",
      "code": "SORT mylist LIMIT 0 5 ALPHA DESC",
      "section_id": "overview"
    },
    {
      "id": "sorting-by-external-keys-ex0",
      "language": "plaintext",
      "code": "SORT mylist BY weight_*",
      "section_id": "sorting-by-external-keys"
    },
    {
      "id": "skip-sorting-the-elements-ex0",
      "language": "plaintext",
      "code": "SORT mylist BY nosort",
      "section_id": "skip-sorting-the-elements"
    },
    {
      "id": "retrieving-external-keys-ex0",
      "language": "plaintext",
      "code": "SORT mylist BY weight_* GET object_*",
      "section_id": "retrieving-external-keys"
    },
    {
      "id": "retrieving-external-keys-ex1",
      "language": "plaintext",
      "code": "SORT mylist BY weight_* GET object_* GET #",
      "section_id": "retrieving-external-keys"
    },
    {
      "id": "storing-the-result-of-a-sort-operation-ex0",
      "language": "plaintext",
      "code": "SORT mylist BY weight_* STORE resultkey",
      "section_id": "storing-the-result-of-a-sort-operation"
    },
    {
      "id": "using-hashes-in-by-and-get-ex0",
      "language": "plaintext",
      "code": "SORT mylist BY weight_*->fieldname GET object_*->fieldname",
      "section_id": "using-hashes-in-by-and-get"
    }
  ]
}
