Skip to content

Commit fe9e235

Browse files
sharetripiqbalJsIqbal
authored andcommitted
Create Notes on SORT
1 parent b174256 commit fe9e235

File tree

1 file changed

+99
-8
lines changed

1 file changed

+99
-8
lines changed

README.md

Lines changed: 99 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -381,13 +381,13 @@ REDIS_PW=
381381
- Normally a hash stores data as key value pair.
382382
- A sorted set stores as members and score.
383383

384-
* but a SCORE data structure in redis sorts using the members of the set, sorted set or list!
384+
* but a `SCORE` data structure in redis sorts using the members of the `set`, `sorted set` or `list`!
385385

386386
- first create some sets and sorted sets:
387387
```bash
388388
HSET books:good title 'Good Book' year 1950
389389
HSET books:bad title 'Bad Book' year 1930
390-
HSET books:ok title 'Ok Book' year 1940
390+
HSET books:ok title 'Ok Book' year 1940
391391

392392
ZADD books:likes 999 good
393393
ZADD books:likes 0 bad
@@ -422,20 +422,111 @@ then apply ->
422422
- create a volatile set with key value structure like:
423423
```bash
424424
good - 1950
425-
bad - 1920
426-
ok - 1960
425+
bad - 1930
426+
ok - 1940
427427
```
428428
- BY means sort the set by descending order like:
429429
```bash
430-
bad - 1920
430+
bad - 1930
431+
ok - 1940
431432
good - 1950
432-
ok - 1960
433433
```
434434
response from redis:
435435
```bash
436436
[
437437
"bad",
438+
"ok",
439+
"good"
440+
]
441+
```
442+
443+
- `SORT books:likes BY books:*->year GET books:*->title` :
444+
* breakdown of the command:
445+
- first replace the * with the member from the sorted set : books:good
446+
- scan redis for the key books:good
447+
- if the key exists then find the value with the year: books:good has year 1950 in it.
448+
- create a volatile set with key value structure like:
449+
```bash
450+
good - 1950
451+
bad - 1930
452+
ok - 1940
453+
```
454+
- BY means sort the set by descending order like:
455+
```bash
456+
bad - 1930
457+
ok - 1940
458+
good - 1950
459+
```
460+
461+
- `GET books:*->title`:
462+
- again scan redis and find the books:good and return the title of the book
463+
- use sorting criteria from BY year
464+
465+
response from redis:
466+
```bash
467+
[
468+
"Bad Book",
469+
"Ok Book",
470+
"Good Book"
471+
]
472+
```
473+
* More examples:
474+
```bash
475+
SORT books:likes BY books:*->year
476+
GET books:*->title
477+
GET books:*->year
478+
```
479+
- response:
480+
```bash
481+
[
482+
"Bad Book",
483+
"1930",
484+
"Ok Book",
485+
"1940",
486+
"Good Book",
487+
"1950"
488+
]
489+
```
490+
491+
* in here # means also give the name of the member with associated record
492+
```bash
493+
SORT books:likes BY books:*->year
494+
GET #
495+
GET books:*->title
496+
GET books:*->year
497+
```
498+
- response:
499+
```bash
500+
[
501+
"bad",
502+
"Bad Book",
503+
"1930",
504+
"ok",
505+
"Ok Book",
506+
"1940",
438507
"good",
439-
"ok"
508+
"Good Book",
509+
"1950"
440510
]
441-
```
511+
```
512+
513+
* in here `nosort` means give the associated record but not based on any sorting criteria:
514+
```bash
515+
SORT books:likes BY nosort
516+
GET #
517+
GET books:*->title
518+
GET books:*->year
519+
```
520+
- response:
521+
```bash
522+
[
523+
"bad",
524+
"Bad Book",
525+
"1930",
526+
"ok",
527+
"Ok Book",
528+
"1940",
529+
"good",
530+
"Good Book",
531+
"1950"
532+
]

0 commit comments

Comments
 (0)