1

考虑下面的哈希

[
{:rank => 3, :time => "3.00 AM" , :name => "JJJ"},
{:rank => 1, :time => "3.05 AM" , :name => "AAA"},
{:rank => 5, :time => "2.55 AM" , :name => "XXX"},
{:rank => 2, :time => "3.10 AM" , :name => "BBB"}
]

我如何按顺序对字段进行排序,以便我将像查询一样获得结果
(考虑我们是否从表中获取 has 中的值)

select * from my_table order by rank, time, name 

如果我不清楚,请发表评论。

谢谢

4

1 回答 1

5
a = [
{:rank => 3, :time => "3.00 AM" , :name => "JJJ"},
{:rank => 1, :time => "3.05 AM" , :name => "AAA"},
{:rank => 5, :time => "2.55 AM" , :name => "XXX"},
{:rank => 2, :time => "3.10 AM" , :name => "BBB"}
]
res = a.sort_by{|t| [t[:rank], t[:time], t[:name]]}
于 2012-04-12T13:26:46.070 回答