我有一个看起来像的哈希
myhash = [{"project 1"=>{"09 Apr 12, Mon"=>"2.75"}}, {"project 1"=>{"11 Apr 12,
Wed"=>"3.200000047683716"}}, {"project 1"=>{"25 Apr 12, Wed"=>"0.5099999904632568"}},
{"project 1"=>{"31 May 12, Thu"=>"1"}}, {"project 1"=>{"01 Jun 12, Fri"=>"1"}}, {"project
2"=> {"01 Jun 12, Fri"=>"0"}}, {"project 1"=>{"04 Jun 12, Mon"=>"0.25"}}]
我想根据它们的键合并内部散列。这样键不会重复
我试过,
myhash.inject{|memo, el| memo.merge( el ){|k, old_v, new_v| old_v.merge(new_v)}}
这是给出正确的结果
=>{"project 2"=>{"01 Jun 12, Fri"=>"0"}, "project 1"=>{"01 Jun 12, Fri"=>"1", "09 Apr
12, Mon"=>"2.75", "11 Apr 12, Wed"=>"3.200000047683716", "31 May 12, Thu"=>"1", "25 Apr
12, Wed"=>"0.5099999904632568", "04 Jun 12, Mon"=>"0.25"}}
但是订单丢失了。如何根据作为哈希内键的日期对哈希进行排序。
或在合并自身时维持秩序。
我正在使用红宝石 1.8.7