简体   繁体   English

如何按值对哈希进行排序

[英]How to sort a hash by values

I was trying to sort a particular hash by values.我试图按值对特定的哈希值进行排序。 I came across a way using the method sort_by .我遇到了一种使用sort_by方法的方法。 But even though I call sort_by on a hash, it returns an array, ie:但即使我在散列上调用sort_by ,它也会返回一个数组,即:

a = {}
a[0] = "c"
a[1] = "b"
a[2] = "a"
a.sort_by{|key, value| value}
# => [[2, "a"], [1, "b"], [0, "c"]]

If I try to convert the result into a hash, I end up with a hash sorted on key, hence the whole purpose of sort_by is lost.如果我尝试将结果转换为散列,我最终会得到一个按键排序的散列,因此sort_by的整个目的就丢失了。 ie: IE:

a = {}
a[0] = "c"
a[1] = "b"
a[2] = "a"
Hash[*a.sort_by{|key, value| value}.flatten]
# => {0=>"c", 1=>"b", 2=>"a"}

Is there a way I can sort a hash by value and yet get the results back in the form of a Hash?有没有一种方法可以按值对散列进行排序,然后以散列的形式返回结果?

I am using 1.8.6 ruby我正在使用 1.8.6 红宝石

A Hash is a collection of key-value pairs.哈希是键值对的集合。 It is similar to an Array, except that indexing is done via arbitrary keys of any object type, not an integer index.它类似于数组,不同之处在于索引是通过任何对象类型的任意键完成的,而不是整数索引。 The order in which you traverse a hash by either key or value may seem arbitrary , and will generally not be in the insertion order.您通过键或值遍历散列的顺序可能看起来是任意的,通常不会按插入顺序排列。

Source: Ruby 1.8.7 docs来源: Ruby 1.8.7 文档

  • Hash preserves order.哈希保留顺序。 It enumerates its elements in the它列举了它的元素

Source: Ruby 1.9.1 changelog来源: Ruby 1.9.1 更新日志

If you want to use a ordered hash in Ruby 1.8, you should look at ActiveSupport's OrderedHash如果你想在 Ruby 1.8 中使用有序散列,你应该查看 ActiveSupport 的OrderedHash

You don't need to be in a Rails project, just include ActiveSupport in your project.您不需要在 Rails 项目中,只需在您的项目中包含 ActiveSupport。

You can use ActiveSupport::OrderedHash for Ruby 1.8:您可以为 Ruby 1.8 使用ActiveSupport::OrderedHash

ActiveSupport::OrderedHash implements a hash that preserves insertion order, as in Ruby 1.9 ActiveSupport::OrderedHash实现了一个保留插入顺序的哈希,就像在 Ruby 1.9 中一样

I don't have 1.8.6 running, but this should work:我没有运行 1.8.6,但这应该有效:

a = {}
a[0] = "c"
a[1] = "b"
a[2] = "a"

ordered = ActiveSupport::OrderedHash[*a.sort_by{|k,v| v}.flatten]
ordered.keys
# => [2, 1, 0], this order is guaranteed

As noted in the quote above hashes in Ruby 1.9 "enumerate their values in the order that the corresponding keys were inserted" , so this is only needed for Ruby 1.8.正如上面引用的 Ruby 1.9 中的哈希“按照插入相应键的顺序枚举它们的值”中所述,因此仅 Ruby 1.8 需要这样做。

Your description is wrong.你的描述有误。

a = {}
a[0] = "c"
a[1] = "b"
a[2] = "a"
Hash[*a.sort_by{|key, value| value}.flatten]

gives:给出:

{
  2 => "a",
  1 => "b",
  0 => "c"
}

which is sorted by value.这是按值排序的。 By the way, your code is redundant.顺便说一下,你的代码是多余的。 A better way to to this is:一个更好的方法是:

Hash[a.sort_by{|_, value| value}]

or要么

Hash[a.sort_by(&:last)]

After Seeing Your Edit You are using a version that does not have ordered hash.看到您的编辑后您正在使用没有有序哈希的版本。 You cannot control the order of the elements.您无法控制元素的顺序。 You cannot do anything with it.你不能用它做任何事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM