什么是按时间排序哈希数组的最佳方法?
[
{:scenario_name=>"Scenario: Two different numbers", :scenario_line=>5, :feature_file=>"multiplication.feature", :time=>"0m2.004s"},
{:scenario_name=>"Scenario: Sum of many numbers", :scenario_line=>6, :feature_file=>"addition.feature", :time=>"0m22.031s"},
{:scenario_name=>"Scenario: Regular numbers", :scenario_line=>5, :feature_file=>"division.feature", :time=>"0m3.004s"},
{:scenario_name=>"Scenario: Subtract two different numbers", :scenario_line=>6, :feature_file=>"subtraction.feature", :time=>"0m3.004s"}
]
所以,我希望按时间排序(按降序排列),如:
[
{:scenario_name=>"Scenario: Sum of many numbers", :scenario_line=>6, :feature_file=>"addition.feature", **:time=>"0m22.031s**"},
{:scenario_name=>"Scenario: Regular numbers", :scenario_line=>5, :feature_file=>"division.feature", **:time=>"0m3.004s**"},
{:scenario_name=>"Scenario: Subtract two different numbers", :scenario_line=>6, :feature_file=>"subtraction.feature", **:time=>"0m3.004s**"},
{:scenario_name=>"Scenario: Two different numbers", :scenario_line=>5, :feature_file=>"multiplication.feature", **:time=>"0m2.004s**"}
]
时间是String
s,所以我之前需要将其转换为Int
吗?或者有什么方法可以对它进行排序吗?
答案 0 :(得分:2)
array.sort_by{|h| h[:time].scan(/[\d.]+/).map(&:to_f)}.reverse
答案 1 :(得分:0)
这个怎么样?
ary = [
{:scenario_name=>"Scenario: Two different numbers", :scenario_line=>5, :feature_file=>"multiplication.feature", :time=>"0m2.004s"},
{:scenario_name=>"Scenario: Sum of many numbers", :scenario_line=>6, :feature_file=>"addition.feature", :time=>"0m22.031s"},
{:scenario_name=>"Scenario: Regular numbers", :scenario_line=>5, :feature_file=>"division.feature", :time=>"0m3.004s"},
{:scenario_name=>"Scenario: Subtract two different numbers", :scenario_line=>6, :feature_file=>"subtraction.feature", :time=>"0m3.004s"}
]
ary = ary.sort_by do |hash|
match = /(\d+)m([\d.]+)s/.match hash[:time]
-(match[1].to_f * 60 + match[2].to_f)
end
ary.each { |hash| p hash }
<强>输出强>
{:scenario_name=>"Scenario: Sum of many numbers", :scenario_line=>6, :feature_file=>"addition.feature", :time=>"0m22.031s"}
{:scenario_name=>"Scenario: Subtract two different numbers", :scenario_line=>6, :feature_file=>"subtraction.feature", :time=>"0m3.004s"}
{:scenario_name=>"Scenario: Regular numbers", :scenario_line=>5, :feature_file=>"division.feature", :time=>"0m3.004s"}
{:scenario_name=>"Scenario: Two different numbers", :scenario_line=>5, :feature_file=>"multiplication.feature", :time=>"0m2.004s"}
答案 2 :(得分:-1)
arr = [
{:scenario_name=>"Scenario: Two different numbers", :scenario_line=>5, :feature_file=>"multiplication.feature", :time=>"0m2.004s"},
{:scenario_name=>"Scenario: Sum of many numbers", :scenario_line=>6, :feature_file=>"addition.feature", :time=>"0m22.031s"},
{:scenario_name=>"Scenario: Regular numbers", :scenario_line=>5, :feature_file=>"division.feature", :time=>"0m3.004s"},
{:scenario_name=>"Scenario: Subtract two different numbers", :scenario_line=>6, :feature_file=>"subtraction.feature", :time=>"0m3.004s"}
]
arr.sort_by{|i| i[:time].scan(/\d+/).map{|i| -i.to_i}}
# => [{:scenario_name=>"Scenario: Sum of many numbers",
# :scenario_line=>6,
# :feature_file=>"addition.feature",
# :time=>"0m22.031s"},
# {:scenario_name=>"Scenario: Subtract two different numbers",
# :scenario_line=>6,
# :feature_file=>"subtraction.feature",
# :time=>"0m3.004s"},
# {:scenario_name=>"Scenario: Regular numbers",
# :scenario_line=>5,
# :feature_file=>"division.feature",
# :time=>"0m3.004s"},
# {:scenario_name=>"Scenario: Two different numbers",
# :scenario_line=>5,
# :feature_file=>"multiplication.feature",
# :time=>"0m2.004s"}]