2
0

SortCommand.html 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
  2. <html>
  3. <head>
  4. <link type="text/css" rel="stylesheet" href="style.css" />
  5. </head>
  6. <body>
  7. <div id="page">
  8. <div id='header'>
  9. <a href="index.html">
  10. <img style="border:none" alt="Redis Documentation" src="redis.png">
  11. </a>
  12. </div>
  13. <div id="pagecontent">
  14. <div class="index">
  15. <!-- This is a (PRE) block. Make sure it's left aligned or your toc title will be off. -->
  16. <b>SortCommand: Contents</b><br>&nbsp;&nbsp;&nbsp;&nbsp;<a href="#Sorting by external keys">Sorting by external keys</a><br>&nbsp;&nbsp;&nbsp;&nbsp;<a href="#Not Sorting at all">Not Sorting at all</a><br>&nbsp;&nbsp;&nbsp;&nbsp;<a href="#Retrieving external keys">Retrieving external keys</a><br>&nbsp;&nbsp;&nbsp;&nbsp;<a href="#Storing the result of a SORT operation">Storing the result of a SORT operation</a><br>&nbsp;&nbsp;&nbsp;&nbsp;<a href="#SORT and Hashes: BY and GET by hash field">SORT and Hashes: BY and GET by hash field</a><br>&nbsp;&nbsp;&nbsp;&nbsp;<a href="#Return value">Return value</a>
  17. </div>
  18. <h1 class="wikiname">SortCommand</h1>
  19. <div class="summary">
  20. </div>
  21. <div class="narrow">
  22. &iuml;&raquo;&iquest;= SORT <i>key</i> <code name="code" class="python">[</code>BY <i>pattern</i><code name="code" class="python">]</code> <code name="code" class="python">[</code>LIMIT <i>start</i> <i>count</i><code name="code" class="python">]</code> <code name="code" class="python">[</code>GET <i>pattern</i><code name="code" class="python">]</code> <code name="code" class="python">[</code>ASC|DESC<code name="code" class="python">]</code> <code name="code" class="python">[</code>ALPHA<code name="code" class="python">]</code> <code name="code" class="python">[</code>STORE <i>dstkey</i><code name="code" class="python">]</code> =
  23. <blockquote>Sort the elements contained in the <a href="Lists.html">List</a>, <a href="Sets.html">Set</a>, or<a href="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>
  24. <pre class="codeblock python" name="code">
  25. SORT mylist
  26. </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>
  27. <pre class="codeblock python python" name="code">
  28. SORT mylist DESC
  29. </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>
  30. <blockquote>Sort is able to limit the number of returned elements using the <b>LIMIT</b> option:</blockquote>
  31. <pre class="codeblock python python python" name="code">
  32. SORT mylist LIMIT 0 10
  33. </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>
  34. <pre class="codeblock python python python python" name="code">
  35. SORT mylist LIMIT 0 10 ALPHA DESC
  36. </pre><blockquote>Will sort <i>mylist</i> lexicographically, in descending order, returning onlythe first 10 elements.</blockquote>
  37. <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>
  38. <h2><a name="Sorting by external keys">Sorting by external keys</a></h2><pre class="codeblock python python python python python" name="code">
  39. SORT mylist BY weight_*
  40. </pre><blockquote>the <b>BY</b> option takes a pattern (<code name="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 <code name="code" class="python">*</code>with the actual value of the elements on the list (1,2,3,4 in our example).</blockquote>
  41. <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>
  42. <h2><a name="Not Sorting at all">Not Sorting at all</a></h2><pre class="codeblock python python python python python python" name="code">
  43. SORT mylist BY nosort
  44. </pre><blockquote>also the <b>BY</b> option can take a &quot;nosort&quot; 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>
  45. <h2><a name="Retrieving external keys">Retrieving external keys</a></h2><pre class="codeblock python python python python python python python" name="code">
  46. SORT mylist BY weight_* GET object_*
  47. </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>
  48. <blockquote>Since Redis &gt;= 1.1 it's possible to also GET the list elements itselfusing the special # pattern:</blockquote>
  49. <pre class="codeblock python python python python python python python python" name="code">
  50. SORT mylist BY weight_* GET object_* GET #
  51. </pre><h2><a name="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 <a href="Lists.html">Redis List</a> in the specified key.An example:</blockquote>
  52. <pre class="codeblock python python python python python python python python python" name="code">
  53. SORT mylist BY weight_* STORE resultkey
  54. </pre><blockquote>An interesting pattern using SORT ... STORE consists in associatingan <a href="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>
  55. <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 <a href="SetnxCommand.html">SETNX</a>).</blockquote>
  56. <h2><a name="SORT and Hashes: BY and GET by hash field">SORT and Hashes: BY and GET by hash field</a></h2>
  57. <blockquote>It's possible to use BY and GET options against Hash fields using the following syntax:</blockquote><pre class="codeblock python python python python python python python python python python" name="code">
  58. SORT mylist BY weight_*-&gt;fieldname
  59. SORT mylist GET object_*-&gt;fieldname
  60. </pre>
  61. <blockquote>The two chars string -&gt; 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><a name="Return value">Return value</a></h2><a href="ReplyTypes.html">Multi bulk reply</a>, specifically a list of sorted elements.
  62. </div>
  63. </div>
  64. </div>
  65. </body>
  66. </html>