小编Tob*_*r00的帖子

从数组的散列中选择属性中的最高值

我有这个哈希:

h = {
  124 => ["shoes", "59.99"],
  456 => ["pants", "49.50"],
  352 => ["socks", "3.99"]
}
Run Code Online (Sandbox Code Playgroud)

每个值都有两个元素.他们是一个名称(如"shoes","pants","socks")和价格(例如"59.99","49.50""3.99").我需要选择价格最高的价值.这124对价格来说至关重要"59.99".如何选择价格最高的哈希?

我试过这个:

h.select{ |x| x[1] }.max
#=> [456, ["pants", "49.50"]]
Run Code Online (Sandbox Code Playgroud)

但这给了我最大值并返回键456.

ruby arrays hash

2
推荐指数
2
解决办法
97
查看次数

标签 统计

arrays ×1

hash ×1

ruby ×1