我理解哈希不应该有任何顺序,我想要做的是从许多不同的API收集数据然后对结果进行排序。我的哈希看起来像这样
hash = \
{{:train_id=>["DALY", "1"]}=>
{0=>
[{:train_no=>0,
:arrival_station=>"MONT",
:destination_departure_time=>"8",
:destination=>"Pittsburg/Bay Point"}],
1=>
[{:train_no=>1,
:arrival_station=>"MONT",
:destination_departure_time=>"23",
:destination=>"Pittsburg/Bay Point"}],
2=>
[{:train_no=>2,
:arrival_station=>"MONT",
:destination_departure_time=>"37",
:destination=>"Pittsburg/Bay Point"}],
3=>
[{:train_no=>0,
:arrival_station=>"POWL",
:destination_departure_time=>"7",
:destination=>"Pittsburg/Bay Point"}],
4=>
[{:train_no=>1,
:arrival_station=>"POWL",
:destination_departure_time=>"21",
:destination=>"Pittsburg/Bay Point"}],
5=>
[{:train_no=>2,
:arrival_station=>"POWL",
:destination_departure_time=>"36",
:destination=>"Pittsburg/Bay Point"}],
6=>
[{:train_no=>0,
:arrival_station=>"CIVC",
:destination_departure_time=>"5",
:destination=>"Pittsburg/Bay Point"}],
7=>
[{:train_no=>1,
:arrival_station=>"CIVC",
:destination_departure_time=>"19",
:destination=>"Pittsburg/Bay Point"}],
8=>
[{:train_no=>2,
:arrival_station=>"CIVC",
:destination_departure_time=>"34",
:destination=>"Pittsburg/Bay Point"}],
9=>
[{:train_no=>0,
:arrival_station=>"16TH",
:destination_departure_time=>"17",
:destination=>"Pittsburg/Bay Point"}],
10=>
[{:train_no=>1,
:arrival_station=>"16TH",
:destination_departure_time=>"32",
:destination=>"Pittsburg/Bay Point"}]},
{:train_id=>["DALY", "5"]}=>{}
}
我需要按以下方式排序:train_no和我使用时
sorted = hash.sort_by{|k,v| v[:train_no] }
我有时会收到错误
ArgumentError: comparison of Hash with Hash failed
这可能是因为其中一个值为null,如果它没有失败,它只返回相同的未排序的排序结果。
关于我做错的任何指导(除了使用哈希来存储我想要排序的东西)?