<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -21,7 +21,6 @@ LONG TERM TODO
  * Add a command to inspect the currently selected DB index
  * Consistent hashing implemented in all the client libraries having an user base
  * SORT: Don't copy the list into a vector when BY argument is constant.
- * SORT ... STORE keyname. Instead to return the SORTed data set it into key.
  * Profiling and optimization in order to limit the CPU usage at minimum
  * Write the hash table size of every db in the dump, so that Redis can resize the hash table just one time when loading a big DB.
  * Elapsed time in logs for SAVE when saving is going to take more than 2 seconds</diff>
      <filename>TODO</filename>
    </modified>
    <modified>
      <diff>@@ -239,6 +239,7 @@ static int cliReadReply(int fd) {
         return 1;
     case '+':
     case ':':
+        printf(&quot;(integer) &quot;);
         return cliReadSingleLineReply(fd);
     case '$':
         return cliReadBulkReply(fd);</diff>
      <filename>redis-cli.c</filename>
    </modified>
    <modified>
      <diff>@@ -172,11 +172,8 @@
 
 /* Sort operations */
 #define REDIS_SORT_GET 0
-#define REDIS_SORT_DEL 1
-#define REDIS_SORT_INCR 2
-#define REDIS_SORT_DECR 3
-#define REDIS_SORT_ASC 4
-#define REDIS_SORT_DESC 5
+#define REDIS_SORT_ASC 1
+#define REDIS_SORT_DESC 2
 #define REDIS_SORTKEY_MAX 1024
 
 /* Log levels */
@@ -4447,7 +4444,7 @@ static void sortCommand(redisClient *c) {
     int limit_start = 0, limit_count = -1, start, end;
     int j, dontsort = 0, vectorlen;
     int getop = 0; /* GET operation counter */
-    robj *sortval, *sortby = NULL;
+    robj *sortval, *sortby = NULL, *storekey = NULL;
     redisSortObject *vector; /* Resulting vector to sort */
 
     /* Lookup the key to sort. It must be of the right types */
@@ -4485,6 +4482,9 @@ static void sortCommand(redisClient *c) {
             limit_start = atoi(c-&gt;argv[j+1]-&gt;ptr);
             limit_count = atoi(c-&gt;argv[j+2]-&gt;ptr);
             j+=2;
+        } else if (!strcasecmp(c-&gt;argv[j]-&gt;ptr,&quot;store&quot;) &amp;&amp; leftargs &gt;= 1) {
+            storekey = c-&gt;argv[j+1];
+            j++;
         } else if (!strcasecmp(c-&gt;argv[j]-&gt;ptr,&quot;by&quot;) &amp;&amp; leftargs &gt;= 1) {
             sortby = c-&gt;argv[j+1];
             /* If the BY pattern does not contain '*', i.e. it is constant,
@@ -4496,18 +4496,6 @@ static void sortCommand(redisClient *c) {
                 REDIS_SORT_GET,c-&gt;argv[j+1]));
             getop++;
             j++;
-        } else if (!strcasecmp(c-&gt;argv[j]-&gt;ptr,&quot;del&quot;) &amp;&amp; leftargs &gt;= 1) {
-            listAddNodeTail(operations,createSortOperation(
-                REDIS_SORT_DEL,c-&gt;argv[j+1]));
-            j++;
-        } else if (!strcasecmp(c-&gt;argv[j]-&gt;ptr,&quot;incr&quot;) &amp;&amp; leftargs &gt;= 1) {
-            listAddNodeTail(operations,createSortOperation(
-                REDIS_SORT_INCR,c-&gt;argv[j+1]));
-            j++;
-        } else if (!strcasecmp(c-&gt;argv[j]-&gt;ptr,&quot;get&quot;) &amp;&amp; leftargs &gt;= 1) {
-            listAddNodeTail(operations,createSortOperation(
-                REDIS_SORT_DECR,c-&gt;argv[j+1]));
-            j++;
         } else {
             decrRefCount(sortval);
             listRelease(operations);
@@ -4614,32 +4602,70 @@ static void sortCommand(redisClient *c) {
     /* Send command output to the output buffer, performing the specified
      * GET/DEL/INCR/DECR operations if any. */
     outputlen = getop ? getop*(end-start+1) : end-start+1;
-    addReplySds(c,sdscatprintf(sdsempty(),&quot;*%d\r\n&quot;,outputlen));
-    for (j = start; j &lt;= end; j++) {
-        listNode *ln;
-        if (!getop) {
-            addReplyBulkLen(c,vector[j].obj);
-            addReply(c,vector[j].obj);
-            addReply(c,shared.crlf);
+    if (storekey == NULL) {
+        /* STORE option not specified, sent the sorting result to client */
+        addReplySds(c,sdscatprintf(sdsempty(),&quot;*%d\r\n&quot;,outputlen));
+        for (j = start; j &lt;= end; j++) {
+            listNode *ln;
+            if (!getop) {
+                addReplyBulkLen(c,vector[j].obj);
+                addReply(c,vector[j].obj);
+                addReply(c,shared.crlf);
+            }
+            listRewind(operations);
+            while((ln = listYield(operations))) {
+                redisSortOperation *sop = ln-&gt;value;
+                robj *val = lookupKeyByPattern(c-&gt;db,sop-&gt;pattern,
+                    vector[j].obj);
+
+                if (sop-&gt;type == REDIS_SORT_GET) {
+                    if (!val || val-&gt;type != REDIS_STRING) {
+                        addReply(c,shared.nullbulk);
+                    } else {
+                        addReplyBulkLen(c,val);
+                        addReply(c,val);
+                        addReply(c,shared.crlf);
+                    }
+                } else {
+                    assert(sop-&gt;type == REDIS_SORT_GET); /* always fails */
+                }
+            }
         }
-        listRewind(operations);
-        while((ln = listYield(operations))) {
-            redisSortOperation *sop = ln-&gt;value;
-            robj *val = lookupKeyByPattern(c-&gt;db,sop-&gt;pattern,
-                vector[j].obj);
-
-            if (sop-&gt;type == REDIS_SORT_GET) {
-                if (!val || val-&gt;type != REDIS_STRING) {
-                    addReply(c,shared.nullbulk);
+    } else {
+        robj *listObject = createListObject();
+        list *listPtr = (list*) listObject-&gt;ptr;
+
+        /* STORE option specified, set the sorting result as a List object */
+        for (j = start; j &lt;= end; j++) {
+            listNode *ln;
+            if (!getop) {
+                listAddNodeTail(listPtr,vector[j].obj);
+                incrRefCount(vector[j].obj);
+            }
+            listRewind(operations);
+            while((ln = listYield(operations))) {
+                redisSortOperation *sop = ln-&gt;value;
+                robj *val = lookupKeyByPattern(c-&gt;db,sop-&gt;pattern,
+                    vector[j].obj);
+
+                if (sop-&gt;type == REDIS_SORT_GET) {
+                    if (!val || val-&gt;type != REDIS_STRING) {
+                        listAddNodeTail(listPtr,createStringObject(&quot;&quot;,0));
+                    } else {
+                        listAddNodeTail(listPtr,val);
+                        incrRefCount(val);
+                    }
                 } else {
-                    addReplyBulkLen(c,val);
-                    addReply(c,val);
-                    addReply(c,shared.crlf);
+                    assert(sop-&gt;type == REDIS_SORT_GET); /* always fails */
                 }
-            } else if (sop-&gt;type == REDIS_SORT_DEL) {
-                /* TODO */
             }
         }
+        dictReplace(c-&gt;db-&gt;dict,storekey,listObject);
+        /* Note: we add 1 because the DB is dirty anyway since even if the
+         * SORT result is empty a new key is set and maybe the old content
+         * replaced. */
+        server.dirty += 1+outputlen;
+        addReplySds(c,sdscatprintf(sdsempty(),&quot;:%d\r\n&quot;,outputlen));
     }
 
     /* Cleanup */</diff>
      <filename>redis.c</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>d8f8b666f451a2116c93ec4ff6207d9ca013855e</id>
    </parent>
  </parents>
  <author>
    <name>antirez</name>
    <email>antirez@gmail.com</email>
  </author>
  <url>http://github.com/antirez/redis/commit/443c6409c326792514ced7a6c68a7f44c47898f7</url>
  <id>443c6409c326792514ced7a6c68a7f44c47898f7</id>
  <committed-date>2009-11-01T06:29:38-08:00</committed-date>
  <authored-date>2009-11-01T06:29:38-08:00</authored-date>
  <message>SORT STORE option</message>
  <tree>334070e4ca8cf89b8e099d857cd27a4e757621d9</tree>
  <committer>
    <name>antirez</name>
    <email>antirez@gmail.com</email>
  </committer>
</commit>
