With this feature Redis will expand the applications in numerous scenarios where right now can only be performed in an SQL-like environment.
SORT Command Usage SORT key [BY pattern [ASC|DESC] [ALPHA] [BY pattern…]] [LIMIT offset count] [GET pattern [GET pattern ...]] [STORE destination]
Documentation The BY option can be used multiple times in order to sort by multiple fields. If the list elements have the same value when ordering by the first BY pattern, then it sorts by the second BY pattern and so on.
The following example will order the list mylist by weight and by title. Sorting first the list of elements by weight, and then by title when the elements have the same weight:
SORT mylist BY weight_* BY title_* ALPHA
Comment From: drednout
Is there any chance to have this functionality be merged into upstream? This feature would be really useful for my current purposes.
Comment From: nathanpalmer
+1
Comment From: gembin
+1 very useful!
Comment From: gpopovic
Awesome!
Comment From: doodirock
225 Was this ever rolled in? I'm not sure why but "By pattern By pattern" isn't working in 2.8 for me. Seems like an amazing feature that greatly extends Sorting to SQL like levels.
Comment From: mattsta
Closing this because #225 is the same improvement.
Looks like a useful addition, but needs a review and mild coding style clean up.
Comment From: doodirock
Thanks for looking at it @mattsta. Would love to see something like this in production. I'll patch it in for now and see how it works.
+1
Comment From: mattsta
If you hit any merge conflicts or anything else messy, open a new PR and we can track it on a non-three-year-old version of Redis. :)
Also, double bonus points if you fix some of the coding style issues (non-cuddling parens, missing spaces after 'if', a few other easy to spot things). :squirrel:
Comment From: doodirock
Good deal, I'm running 2.6 on my local so I'll upgrade and give it all a whirl. I'll look into those pesky style issues too.
Comment From: doodirock
Patching into 2.6 works fine, but it looks like there a quite a few diffs in the latest version where a simple merge isn't quite possible (at least with my skillset).
Comment From: mattsta
Can you push your successful 2.6 merge to a public repo? If you've already merged it into 2.6 we can use that to (try and) help bring it into 2.8+.
Comment From: doodirock
Sure thing mattsta. I'll get that up sometime this week so you can take a look at it. Thanks for the help.
Comment From: emiltin
what happened to this? seems very useful!
Comment From: BharathZoho
This is extremely useful, has it made it into the main branch?
Comment From: ghost
+1
Comment From: ssorren
+10
Comment From: fcerbell
It would help me a lot, too.
Comment From: wajdijurry
+1
Comment From: cscan
This is my test case:
127.0.0.1:6380> hmset test:01 f1 345 f2 A
127.0.0.1:6380> hmset test:02 f1 444 f2 B
127.0.0.1:6380> hmset test:03 f1 776 f2 B
127.0.0.1:6380> hmset test:04 f1 111 f2 B
127.0.0.1:6380> hmset test:05 f1 999 f2 A
127.0.0.1:6380> sadd testset test:01
127.0.0.1:6380> sadd testset test:02
127.0.0.1:6380> sadd testset test:03
127.0.0.1:6380> sadd testset test:04
127.0.0.1:6380> sadd testset test:05
127.0.0.1:6380> sort testset by *->f2 alpha by *->f1
1) "test:04"
2) "test:01"
3) "test:02"
4) "test:03"
5) "test:05"
BTW, the result should be:
1) "test:01"
2) "test:05"
3) "test:04"
4) "test:02"
5) "test:03"
+11