如何通过键中的值对Redis哈希进行排序 [英] How to sort a redis hash by values in keys

查看:141
本文介绍了如何通过键中的值对Redis哈希进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

redis中有没有一种好的方法来获取按值排序的哈希中的键?我查看了文档,但没有找到简单的方法.

is there a good way in redis to get keys in a hash sorted by values? I've looked at the documentation and haven't found a straightforward way.

也可以有人请解释在Redis中如何实现排序,以及

Also could someone please explain how sorting is achieved in redis, and what this documentation is trying to say?

我有一个非常简单的哈希结构,如下所示:

I have a very simple hash structure which is something like this:

工资"-员工_1"-工资金额"

"salaries" - "employee_1" - "salary_amount"

我希望您能得到详细的解释.

I'd appreciate a detailed explanation.

推荐答案

来自 SORT 的文档页面:

From SORT's documentation page:

返回或存储包含在键中的列表,集合或排序集合中的元素

Returns or stores the elements contained in the list, set or sorted set at key

因此,您不能真正使用它按哈希数据结构中的字段值对字段进行排序.为了实现您的目标,您应该在获取哈希的内容之后对应用程序的代码进行排序,或者为此目的使用Redis嵌入的Lua脚本.

So you can't really use it to sort the fields by their values in a Hash data structure. To achieve your goal you should either do the sorting in your application's code after getting the Hash's contents or use a Redis-embedded Lua script for that purpose.

与@OfirLuzon交谈后,我们意识到还有另一种甚至更好的方法,那就是为此使用更合适的数据结构.而不是将薪水存储在哈希中,您应该考虑使用排序集,其中每个成员都是员工ID,而分数是相关薪水.这将为您提供免费"的订购,范围和分页:)

After speaking with @OfirLuzon we realized that there is another, perhaps even preferable, approach which would be to use a more suitable data structure for this purpose. Instead of storing the salaries in a Hash, you should consider using a Sorted Set in which each member is an employee ID and the score is the relevant salary. This will give you ordering, ranges and paging for "free" :)

这篇关于如何通过键中的值对Redis哈希进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆