简体   繁体   English

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

[英]Selecting single hash from array where one hash element has the highest value

From the following array of hashes, how can I select a single hash where timestamp has the highest value? 从以下哈希数组中,如何选择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"}
]

Here you can see: 在这里您可以看到:

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

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

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