<b>SortCommand: Contents</b><br> <ahref="#Sorting by external keys">Sorting by external keys</a><br> <ahref="#Not Sorting at all">Not Sorting at all</a><br> <ahref="#Retrieving external keys">Retrieving external keys</a><br> <ahref="#Storing the result of a SORT operation">Storing the result of a SORT operation</a><br> <ahref="#SORT and Hashes: BY and GET by hash field">SORT and Hashes: BY and GET by hash field</a><br> <ahref="#Return value">Return value</a>
<blockquote>Sort the elements contained in the <ahref="Lists.html">List</a>, <ahref="Sets.html">Set</a>, or<ahref="SortedSets.html">Sorted Set</a> value at <i>key</i>. By defaultsorting is numeric with elements being compared as double precisionfloating point numbers. This is the simplest form of SORT:</blockquote>
<preclass="codeblock python"name="code">
SORT mylist
</pre><blockquote>Assuming mylist contains a list of numbers, the return value will bethe list of numbers ordered from the smallest to the biggest number.In order to get the sorting in reverse order use <b>DESC</b>:</blockquote>
<preclass="codeblock python python"name="code">
SORT mylist DESC
</pre><blockquote>The <b>ASC</b> option is also supported but it's the default so you don'treally need it.If you want to sort lexicographically use <b>ALPHA</b>. Note that Redis isutf-8 aware assuming you set the right value for the LC_COLLATEenvironment variable.</blockquote>
<blockquote>Sort is able to limit the number of returned elements using the <b>LIMIT</b> option:</blockquote>
</pre><blockquote>In the above example SORT will return only 10 elements, starting fromthe first one (start is zero-based). Almost all the sort options canbe mixed together. For example the command:</blockquote>
</pre><blockquote>Will sort <i>mylist</i> lexicographically, in descending order, returning onlythe first 10 elements.</blockquote>
<blockquote>Sometimes you want to sort elements using external keys as weights tocompare instead to compare the actual List Sets or Sorted Set elements.For example the list <i>mylist</i> may contain the elements 1, 2, 3, 4, thatare just unique IDs of objects stored at object_1, object_2, object_3and object_4, while the keys weight_1, weight_2, weight_3 and weight_4can contain weights we want to use to sort our list of objectsidentifiers. We can use the following command:</blockquote>
<h2><aname="Sorting by external keys">Sorting by external keys</a></h2><preclass="codeblock python python python python python"name="code">
SORT mylist BY weight_*
</pre><blockquote>the <b>BY</b> option takes a pattern (<codename="code"class="python">weight_*</code> in our example) that is usedin order to generate the key names of the weights used for sorting.Weight key names are obtained substituting the first occurrence of <codename="code"class="python">*</code>with the actual value of the elements on the list (1,2,3,4 in our example).</blockquote>
<blockquote>Our previous example will return just the sorted IDs. Often it isneeded to get the actual objects sorted (object_1, ..., object_4 in theexample). We can do it with the following command:</blockquote>
<h2><aname="Not Sorting at all">Not Sorting at all</a></h2><preclass="codeblock python python python python python python"name="code">
SORT mylist BY nosort
</pre><blockquote>also the <b>BY</b> option can take a "nosort" specifier. This is useful if you want to retrieve a external key (using GET, read below) but you don't want the sorting overhead.</blockquote>
</pre><blockquote>Note that <b>GET</b> can be used multiple times in order to get more keys forevery element of the original List, Set or Sorted Set sorted.</blockquote>
<blockquote>Since Redis >= 1.1 it's possible to also GET the list elements itselfusing the special # pattern:</blockquote>
</pre><h2><aname="Storing the result of a SORT operation">Storing the result of a SORT operation</a></h2><blockquote>By default SORT returns the sorted elements as its return value.Using the <b>STORE</b> option instead to return the elements SORT willstore this elements as a <ahref="Lists.html">Redis List</a> in the specified key.An example:</blockquote>
</pre><blockquote>An interesting pattern using SORT ... STORE consists in associatingan <ahref="ExpireCommand.html">EXPIRE</a> timeout to the resulting key so that inapplications where the result of a sort operation can be cached forsome time other clients will use the cached list instead to call SORTfor every request. When the key will timeout an updated version ofthe cache can be created using SORT ... STORE again.</blockquote>
<blockquote>Note that implementing this pattern it is important to avoid that multipleclients will try to rebuild the cached version of the cacheat the same time, so some form of locking should be implemented(for instance using <ahref="SetnxCommand.html">SETNX</a>).</blockquote>
<blockquote>It's possible to use BY and GET options against Hash fields using the following syntax:</blockquote><preclass="codeblock python python python python python python python python python python"name="code">
<blockquote>The two chars string -> is used in order to signal the name of the Hash field. The key is substituted as documented above with sort BY and GET against normal keys, and the Hash stored at the resulting key is accessed in order to retrieve the specified field.</blockquote><h2><aname="Return value">Return value</a></h2><ahref="ReplyTypes.html">Multi bulk reply</a>, specifically a list of sorted elements.