hset cannot be sorted directly. There is an alternative to achieve the equivalent of hset sorting, which is to use the sort function. Sort can generally only sort lists, sets and zsets, but it can be modified. The usage of sort is as follows:
SORT key [BY pattern] [LIMIT offset count] [GET pattern [GET pattern ...]] [ASC | DESC] [ALPHA] [STORE destination]
Pay attention to its by and store parameters. by is used to determine which field to sort by, and store can save the sorting results into a list. You can first save all hash keys into a set, for example, the key name of this set is myhashes, then sort according to the score field of each element in the set (that is, each hash), and finally save the sorting results in In the list with the key name myresult. The sorting result here is of course the key names of all elements in the set (that is, all hash keys). You can get the key names of all elements in the set by adding # to the GET parameter. It is written like this:
SORT myhashes BY *->score GET # STORE myresult
SORT will replace the * in the BY parameter with each value in myhashes (that is, each hash key name), and obtain its value based on the fields given after ->, and finally compare each value in myhashes based on these field values. Values are sorted. In this way, myresult contains hash key names that have been sorted according to score.
Finally, I think there is something wrong with your hash design. Hash is more like a row in a SQL database, with multiple fields, and each field has a corresponding value. If you only have a score field, consider using zset.
hset cannot be sorted directly. There is an alternative to achieve the equivalent of hset sorting, which is to use the sort function. Sort can generally only sort lists, sets and zsets, but it can be modified. The usage of sort is as follows:
SORT key [BY pattern] [LIMIT offset count] [GET pattern [GET pattern ...]] [ASC | DESC] [ALPHA] [STORE destination]
Pay attention to its by and store parameters. by is used to determine which field to sort by, and store can save the sorting results into a list. You can first save all hash keys into a set, for example, the key name of this set is myhashes, then sort according to the score field of each element in the set (that is, each hash), and finally save the sorting results in In the list with the key name myresult. The sorting result here is of course the key names of all elements in the set (that is, all hash keys). You can get the key names of all elements in the set by adding # to the GET parameter. It is written like this:
SORT myhashes BY *->score GET # STORE myresult
SORT will replace the * in the BY parameter with each value in myhashes (that is, each hash key name), and obtain its value based on the fields given after ->, and finally compare each value in myhashes based on these field values. Values are sorted. In this way, myresult contains hash key names that have been sorted according to score.
Finally, I think there is something wrong with your hash design. Hash is more like a row in a SQL database, with multiple fields, and each field has a corresponding value. If you only have a score field, consider using zset.
HashSet
没法直接排。得单独维护一个SortedSet
, only the user ID and corresponding score are stored in it.I don’t know if that’s what it means
Just upload the code
Document
http://redis.cn/commands/sort.html
Hash cannot be sorted.
If you need to sort, please use ZSET operation, ZADD SCORES MEMBERS
The idea is:
PS it again, if your HASH is only used to store a hashkey SCORES is not recommended to save like this..