在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
people = { :fred => 23, :joan => 18, :pete => 54 } 年龄排序: people.values.sort # => [18, 23, 54] 姓名排序:people.sort_by { |name, age| age } # => [[:joan, 18], [:fred, 23], [:pete, 54]] 或者 people.sort
一个复杂点的hash people = { :fred => { :name => "Fred", :age => 23 }, :joan => { :name => "Joan", :age => 18 }, :pete => { :name => "Pete", :age => 54 } }
people.sort_by { |k, v| v[:age] } # => [[:joan, {:name=>"Joan", :age=>18}], [:fred, {:name=>"Fred", :age=>23}], [:pete, {:name=>"Pete", :age=>54}]]
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论