w3resource

Redis Keys: SORT

Redis SORT Command

The usage is simple sort key.

Here you write with a key data type is a list, set, sorted set.

data (member) to put the data on the sort not seen, let's set sort.

Here are some common uses and scenarios for the "SORT" command:

  • Sorting Lists: Sort elements within a list based on element values or external criteria.
  • Set Operations: Retrieve and sort elements from sets based on scores or member values.
  • Pagination: Implement pagination by combining sorting with LIMIT and OFFSET options.
  • Data Transformation: Sort and retrieve data in a different order than stored for presentation or processing purposes.

Syntax:

SORT key [BY pattern] [LIMIT offset count] [GET pattern [GET pattern ...]] [ASC | DESC] [ALPHA] [STORE destination]

Available since

1.0.0.

Return Value

Returns or stores the elements contained in the list, set or sorted set at the key. By default, sorting is numeric and elements are compared by their value interpreted as double precision floating point number.

  • Since in the second call, the returned cursor is 0
  • SCAN until the returned cursor is 0 again

Return Value Type

Integer

Example: Redis SORT alpha

127.0.0.1:6379> SADD xurl Facebook.com Buddy.com Yahoo.com Youtube.com Example.com
(integer) 5
127.0.0.1:6379> SORT xurl alpha
1) "Buddy.com"
2) "Example.com"
3) "Facebook.com"
4) "Yahoo.com"
5) "Youtube.com"

Example: Redis SORT alpha desc

127.0.0.1:6379> SORT xurl alpha desc
1) "Youtube.com"
2) "Yahoo.com"
3) "Facebook.com"
4) "Example.com"
5) "Buddy.com"

Example: Redis SORT limit offset count

127.0.0.1:6379> SORT xurl alpha limit 0 3
1) "Buddy.com"
2) "Example.com"
3) "Facebook.com"
127.0.0.1:6379> SORT xurl alpha limit 0 10
1) "Buddy.com"
2) "Example.com"
3) "Facebook.com"
4) "Yahoo.com"
5) "Youtube.com"
127.0.0.1:6379> SORT xurl alpha limit 3 10
1) "Yahoo.com"
2) "Youtube.com"

Use get option

If you get used to query the value of the other key

For example, when compared to Google.com and rank-Google.com of xurl to query the value of the use rank-Google.com get rank-*.

If you are thinking to join in a relational database.

Enter the web site ranking to prepare data

Example: Redis SORT rank-* alpha

127.0.0.1:6379> MSET rank-Google.com 1 rank-Facebook.com 2 rank-Youtube.com 3 rank-Buddy.com 4 rank-Example.com 5
OK
127.0.0.1:6379> SORT xurl get rank-* alpha
1) "4"
2) "5"
3) "2"
4) "1"
5) "3"

Previous: SCAN
Next: TTL



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/redis/redis-sort-key.php