在 ruby 中对多维数组进行排序 [英] sorting a multidimensional array in ruby
本文介绍了在 ruby 中对多维数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下数组:
[["2010-01-10", 2], ["2010-01-09", 5], ["2009-12-11", 3], ["2009-12-12", 12], ["2009-12-13", 0]]
我只想按每组中的第二个值对其进行排序并返回最高的值,就像我希望输出为 12
和上面给定的输入一样.
I just want to sort it by the second value in each group and return the highest one, like i want to the output to be 12
with the given input above.
更新
我可能会补充说,我使用 to_a
从散列中将其制作成一个数组,因此如果可以使用散列来做同样的事情,那就更好了.
I might add that I made this into an array using to_a
, from a hash, so if there is away to do the same with a hash that would be even better.
推荐答案
在你的哈希上使用这个:
Use this on your hash:
hash.values.max
如果只需要最高的元素,就不需要排序了!
If you only need the highest element, there is no need to sort it!
这篇关于在 ruby 中对多维数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文