从数组中选择单个哈希,其中一个哈希元素具有最高值

时间:2013-11-01 14:11:35

标签: ruby arrays hash

从以下哈希数组中,如何选择timestamp具有最高值的单个哈希?

data = [
  {"id" => 1, "timestamp" => 1383314276, "data" => "bea7c82f-f4b2-492a-aba3-033b1a54d9d0"},
  {"id" => 2, "timestamp" => 1383314386, "data" => "64ed2ed9-763d-443f-a74e-e7f10cbe783e"},
  {"id" => 3, "timestamp" => 1383314331, "data" => "f8cfaa99-ffe0-4d88-8fac-37e4ce462d3a"}
]

1 个答案:

答案 0 :(得分:5)

在这里你可以看到:

 data.max_by{|e| e["timestamp"] }
 # >> {"id"=>2, "timestamp"=>1383314386, "data"=>"64ed2ed9-763d-443f-a74e-e7f10cbe783e"}