]> git.saurik.com Git - redis.git/blame - doc/SortCommand.html
first commit
[redis.git] / doc / SortCommand.html
CommitLineData
ed9b544e 1
2<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
3<html>
4 <head>
5 <link type="text/css" rel="stylesheet" href="style.css" />
6 </head>
7 <body>
8 <div id="page">
9
10 <div id='header'>
11 <a href="index.html">
12 <img style="border:none" alt="Redis Documentation" src="redis.png">
13 </a>
14 </div>
15
16 <div id="pagecontent">
17 <div class="index">
18<!-- This is a (PRE) block. Make sure it's left aligned or your toc title will be off. -->
19<b>SortCommand: Contents</b><br>&nbsp;&nbsp;<a href="#SORT _key_ BY _pattern_ LIMIT _start_ _end_ GET _pattern_ ASC|DESC ALPHA">SORT _key_ BY _pattern_ LIMIT _start_ _end_ GET _pattern_ ASC|DESC ALPHA</a><br>&nbsp;&nbsp;&nbsp;&nbsp;<a href="#Return value">Return value</a><br>&nbsp;&nbsp;&nbsp;&nbsp;<a href="#See Also">See Also</a>
20 </div>
21
22 <h1 class="wikiname">SortCommand</h1>
23
24 <div class="summary">
25
26 </div>
27
28 <div class="narrow">
29 <h1><a name="SORT _key_ BY _pattern_ LIMIT _start_ _end_ GET _pattern_ ASC|DESC ALPHA">SORT _key_ BY _pattern_ LIMIT _start_ _end_ GET _pattern_ ASC|DESC ALPHA</a></h1>
30<blockquote>Sort the elements contained in the List or Set 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>
31<pre class="codeblock python" name="code">
32SORT mylist
33</pre><blockquote>Assuming mylist contains a list of numbers, the return value will bethe list of numbers ordered from the smallest to the bigger number.In order to get the sorting in reverse order use DESC:</blockquote>
34<pre class="codeblock python python" name="code">
35SORT mylist DESC
36</pre><blockquote>ASC is also supported but it's the default so you don't really need it.If you want to sort lexicographically use ALPHA. Note that Redis isutf-8 aware assuming you set the right value for the LC_COLLATEenvironment variable.</blockquote>
37<blockquote>Sort is able to limit the number of results using the LIMIT option:</blockquote>
38<pre class="codeblock python python python" name="code">
39SORT mylist LIMIT 0 10
40</pre><blockquote>In the above example SORT will return only 10 elements, starting fromthe first one (star is zero-based). Almost all the sort options canbe mixed together. For example:</blockquote>
41<pre class="codeblock python python python python" name="code">
42SORT mylist LIMIT 0 10 ALPHA DESC
43</pre><blockquote>Will sort <i>mylist</i> lexicographically, in descending order, returning onlythe first 10 elements.</blockquote>
44<blockquote>Sometimes you want to sort elements using external keys as weights tocompare instead to compare the actual List or Set elements. For examplethe list <i>mylist</i> may contain the elements 1, 2, 3, 4, that are justthe 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 the list of objectsidentifiers. We can use the following command:</blockquote>
45<pre class="codeblock python python python python python" name="code">
46SORT mylist BY weight_*
47</pre><blockquote>the BY 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>
48<blockquote>Still 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>
49<pre class="codeblock python python python python python python" name="code">
50SORT mylist BY weight_* GET object_*
51</pre><blockquote>Note that GET can be used multiple times in order to get more keys forevery element of the original List or Set sorted.</blockquote>
52<h2><a name="Return value">Return value</a></h2><a href="ReplyTypes.html">Multi bulk reply</a>, specifically a list of sorted elements.<h2><a name="See Also">See Also</a></h2>
53<ul><li> <a href="SinterCommand.html">SINTER</a></li></ul>
54 </div>
55
56 </div>
57 </div>
58 </body>
59</html>
60