有没有办法从散列中返回排序的键列表? [英] Is there a way to return a sorted key list from a hash?

查看:44
本文介绍了有没有办法从散列中返回排序的键列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 redis 的新手,阅读文档时我无法找到解决问题的方法.

I'm new to redis and reading the documentation I'm not able to find a solution to my problem.

我有一个包含姓名和电话号码的散列,我想获得散列中键的排序列表.

I have a hash with names and phone numbers and I would like to get the a sorted list of the keys in the hash.

所以我的哈希(电话簿)看起来像这样:

So my hash (phonebook) looks like this:

 Andrew -> 9999
 Sam    -> 6666
 Eddy   -> 5555

如果我运行 hkeys phonebook 我得到这个(键在存储时返回):

If I run hkeys phonebook I get this (keys are returned as they are stored):

 Andrew
 Sam
 Eddy

我想得到这个(有序密钥):

And I would like to get this (ordered keys):

 Andrew
 Eddy
 Sam

我该如何存档?我是否使用了正确的数据结构?

How could I archive this? Am I using the correct data structure?

推荐答案

你可以使用排序集来实现这一点,而不是哈希,并且不需要维护并行列表;它全部包含在一个结构中...

You can use a sorted set to achieve this, not a hash, and you don't need to maintain a parallel list; it's all contained in a single structure...

填充排序集...

> zadd ss:phonebook 9999 Andrew
> zadd ss:phonebook 4444 Sam
> zadd ss:phonebook 3333 Bob
> zadd ss:phonebook 7777 Maria
> zadd ss:phonebook 8888 Sophia

由于 ss:phonebook 包含字符串值(名称),并且您想按字典顺序对它们进行排序,请使用 ALPHA 修饰符:

Since ss:phonebook contains string values (the names), and you want to sort them lexicographically, use the ALPHA modifier:

> SORT ss:phonebook ALPHA
1) "Andrew"
2) "Bob"
3) "Maria"
4) "Sam"
5) "Sophia"

希望能帮到你...

这篇关于有没有办法从散列中返回排序的键列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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