【问题标题】:I want to sort the hash in the array我想对数组中的哈希进行排序
【发布时间】:2016-09-27 05:57:39
【问题描述】:
a = [{"name"=> "ted","age"=>1}, 
     {"name"=> "mika","age"=>2},
     {"name"=> "bob","age"=>0}]

如何使用 ruby​​ 程序按年龄顺序(升序)对 a 进行排序?

我想成为下面的人。

a = [{"name"=> "bob","age"=>0},
     {"name"=> "ted","age"=>1},
     {"name"=> "mika","age"=>2}]

【问题讨论】:

标签: arrays ruby sorting hash


【解决方案1】:

正如 Cary Swoveland 在评论中所说,

a.sort_by { |h| h["age"] }

我可以像上面那样做。谢谢。

【讨论】:

    猜你喜欢
    • 2019-04-25
    • 2012-01-21
    • 1970-01-01
    • 2014-03-05
    • 2011-03-10
    • 2010-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多