diff --git a/README.markdown b/README.markdown index 0aa273b5..67e6849d 100644 --- a/README.markdown +++ b/README.markdown @@ -1110,7 +1110,7 @@ $redis->flushAll();
     'by' => 'some_pattern_*',
     'limit' => array(0, 1),
-    'get' => 'some_other_pattern_*',
+    'get' => 'some_other_pattern_*' or an array of patterns,
     'sort' => 'asc' or 'desc',
     'alpha' => TRUE,
     'store' => 'external-key'
diff --git a/redis.c b/redis.c
index 841b23c2..3fc4c49f 100755
--- a/redis.c
+++ b/redis.c
@@ -2060,18 +2060,51 @@ PHP_METHOD(Redis, sort) {
 
         if ((zend_hash_find(Z_ARRVAL_P(z_array), "get", sizeof("get"), (void **) &z_cur) == SUCCESS
          || zend_hash_find(Z_ARRVAL_P(z_array), "GET", sizeof("GET"), (void **) &z_cur) == SUCCESS)
-                        && Z_TYPE_PP(z_cur) == IS_STRING) {
+                        && (Z_TYPE_PP(z_cur) == IS_STRING || Z_TYPE_PP(z_cur) == IS_ARRAY)) {
 
-            old_cmd = cmd;
-            cmd_len = redis_cmd_format(&cmd, "%s"
-                                             "$3" _NL
-                                             "GET" _NL
-                                             "$%d" _NL
-                                             "%s" _NL
-                                             , cmd, cmd_len
-                                             , Z_STRLEN_PP(z_cur), Z_STRVAL_PP(z_cur), Z_STRLEN_PP(z_cur));
-            elements += 2;
-            efree(old_cmd);
+            if(Z_TYPE_PP(z_cur) == IS_STRING) {
+                old_cmd = cmd;
+                cmd_len = redis_cmd_format(&cmd, "%s"
+                                                 "$3" _NL
+                                                 "GET" _NL
+                                                 "$%d" _NL
+                                                 "%s" _NL
+                                                 , cmd, cmd_len
+                                                 , Z_STRLEN_PP(z_cur), Z_STRVAL_PP(z_cur), Z_STRLEN_PP(z_cur));
+                elements += 2;
+                efree(old_cmd);
+            } else if(Z_TYPE_PP(z_cur) == IS_ARRAY) { // loop over the strings in that array and add them as patterns
+
+                HashTable *keytable = Z_ARRVAL_PP(z_cur);
+                for(zend_hash_internal_pointer_reset(keytable);
+                    zend_hash_has_more_elements(keytable) == SUCCESS;
+                    zend_hash_move_forward(keytable)) {
+
+                    char *key, *val;
+                    int key_len, val_len;
+                    unsigned long idx;
+                    int type;
+                    zval **z_value_pp;
+
+                    type = zend_hash_get_current_key_ex(keytable, &key, &key_len, &idx, 0, NULL);
+                    if(zend_hash_get_current_data(keytable, (void**)&z_value_pp) == FAILURE) {
+                        continue; 	/* this should never happen, according to the PHP people. */
+                    }
+
+                    if(Z_TYPE_PP(z_value_pp) == IS_STRING) {
+                        old_cmd = cmd;
+                        cmd_len = redis_cmd_format(&cmd, "%s"
+                                                         "$3" _NL
+                                                         "GET" _NL
+                                                         "$%d" _NL
+                                                         "%s" _NL
+                                                         , cmd, cmd_len
+                                                         , Z_STRLEN_PP(z_value_pp), Z_STRVAL_PP(z_value_pp), Z_STRLEN_PP(z_value_pp));
+                        elements += 2;
+                        efree(old_cmd);
+                    }
+                }
+            }
         }
 
         if ((zend_hash_find(Z_ARRVAL_P(z_array), "alpha", sizeof("alpha"), (void **) &z_cur) == SUCCESS
@@ -2122,6 +2155,7 @@ PHP_METHOD(Redis, sort) {
     }
 
     /* complete with prefix */
+
     old_cmd = cmd;
     cmd_len = redis_cmd_format(&cmd, "*%d" _NL "%s",
                     elements, cmd, cmd_len);
diff --git a/tests/TestRedis.php b/tests/TestRedis.php
index 6eda9732..cc2990ec 100644
--- a/tests/TestRedis.php
+++ b/tests/TestRedis.php
@@ -573,7 +573,7 @@ class Redis_Test extends PHPUnit_Framework_TestCase
 
 	$this->assertEquals(array_slice($byAgeAsc, 0, 2), $this->redis->sortAsc('person:id', 'person:age_*', 'person:name_*', 0, 2));
 	$this->assertEquals(array_slice($byAgeAsc, 0, 2), $this->redis->sort('person:id', array('by' => 'person:age_*', 'get' => 'person:name_*', 'limit' => array(0, 2), 'sort' => 'asc')));
-	return;
+
 	$this->assertEquals(array_slice($byAgeAsc, 1, 2), $this->redis->sortAsc('person:id', 'person:age_*', 'person:name_*', 1, 2));
 	$this->assertEquals(array_slice($byAgeAsc, 1, 2), $this->redis->sort('person:id', array('by' => 'person:age_*', 'get' => 'person:name_*', 'limit' => array(1, 2), 'sort' => 'asc')));
 	$this->assertEquals(array_slice($byAgeAsc, 0, 3), $this->redis->sortAsc('person:id', 'person:age_*', 'person:name_*', NULL, 3)); // NULL is transformed to 0 if there is something after it.
@@ -586,6 +586,9 @@ class Redis_Test extends PHPUnit_Framework_TestCase
 	$this->assertEquals($agesBySalaryAsc, $this->redis->sortAsc('person:id', 'person:salary_*', 'person:age_*'));
 	$this->assertEquals($agesBySalaryAsc, $this->redis->sort('person:id', array('by' => 'person:salary_*', 'get' => 'person:age_*', 'sort' => 'asc')));
 
+	$agesAndSalaries = $this->redis->sort('person:id', array('by' => 'person:salary_*', 'get' => array('person:age_*', 'person:salary_*'), 'sort' => 'asc'));
+	$this->assertEquals(array('34', '2000', '27', '2500', '25', '2800', '41', '3100'), $agesAndSalaries);
+
 
 	// sort non-alpha doesn't change all-string lists
 	// list → [ghi, def, abc]