English 中文(简体)
按嵌套哈希中的项的值对其进行排序
原标题:Sort items in a nested hash by their values
  • 时间:2011-02-07 17:50:16
  •  标签:
  • ruby
  • sorting

我收到了一个嵌套的散列,需要按其值进行排序。例如:

@foo = {"a"=>{"z"=>5, "y"=>3, "x"=>88}, "b"=>{"a"=>2, "d"=>-5}}

运行以下程序时:

@foo["a"].sort{|a,b| a[1]<=>b[1]}

我得到:

[["y", 3], ["z", 5], ["x", 88]]

这太棒了,这正是我想要的。问题是我并不总是知道发送给我的所有密钥是什么,所以我需要某种循环。我试着做了以下事情:

@foo.each do |e|   
  e.sort{|a,b| a[1]<=>b[1]}
end

这对我来说是有意义的,因为如果我手动调用@foo.first[0],我会得到

"a"

并且@foo.first[1]返回

{"z"=>5, "y"=>3, "x"=>8}

但由于某种原因,这没有正确排序(例如根本没有排序)。我认为这是因为each对整个散列对象调用sort,而不是对“a”的值调用sort。如何访问嵌套哈希的值而不知道它的密钥是什么?

最佳回答

您可能想要像这样循环散列:

@foo.each do |key, value|
  @foo[key] = value.sort{ |a,b| a[1]<=>b[1] }
end
问题回答
@foo = {"a"=>{"z"=>5, "y"=>3, "x"=>88}, "b"=>{"a"=>2, "d"=>-5}}
@bar = Hash[ @foo.map{ |key,values| [ key, values.sort_by(&:last) ] } ]

或者,通过一条不那么棘手的路径:

@bar = {}
@foo.each do |key,values|
  @bar[key] = values.sort_by{ |key,value| value }
end

在这两种情况下,@bar都是:

p @bar
#=> {
#=>   "a"=>[["y", 3], ["z", 5], ["x", 88]],
#=>   "b"=>[["d", -5], ["a", 2]]
#=> }

我的同事提出了一个稍微灵活一点的解决方案,可以递归地对任何深度的数组进行排序:

def deep_sort_by(&block)
  Hash[self.map do |key, value|
    [if key.respond_to? :deep_sort_by
      key.deep_sort_by(&block)
    else
      key
    end,

    if value.respond_to? :deep_sort_by
      value.deep_sort_by(&block)
    else
      value
    end]

  end.sort_by(&block)]
end

你可以将它注入所有哈希中,然后这样调用它:

myMap.deep_sort_by { |obj| obj }

对于一个数组,代码将是类似的。我们将其发布为宝石供其他人使用,请参阅博客文章了解更多详细信息。

免责声明:我在这家公司工作。

在您的示例中,e是一个包含[key,value]对的临时数组。在这种情况下,是字符键和嵌套哈希。因此,e.sort{|a,b|…}将尝试将字符与哈希进行比较,但由于运行时错误而失败。我认为您可能是想键入e[1].sort{…}。但即使这样也不会正确工作,因为您不会将排序后的哈希存储在任何位置:@foo.each返回原始的@foo并保持不变。

更好的解决方案是@Pan Thomakos建议的解决方案:

@foo.each do |key, value|
  @foo[key] = value.sort{ |a,b| a[1]<=>b[1] }
end




相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签