Ruby-按ID排序并按哈希中的日期分组

问题描述 投票:1回答:1

我一直在为此苦苦挣扎,无法获得令人满意的结果。我有这个哈希:

{"idprj"=>13351, "prevInicioStr"=>"21/10/2019"}
{"idprj"=>14473, "prevInicioStr"=>"22/10/2019"}
{"idprj"=>14165, "prevInicioStr"=>"23/10/2019"}
{"idprj"=>14165, "prevInicioStr"=>"23/10/2019"}
{"idprj"=>14165, "prevInicioStr"=>"28/10/2019"}
{"idprj"=>14165, "prevInicioStr"=>"28/10/2019"}
{"idprj"=>14518, "prevInicioStr"=>"29/10/2019"}
{"idprj"=>14518, "prevInicioStr"=>"29/10/2019"}
{"idprj"=>13351, "prevInicioStr"=>"05/11/2019"}
{"idprj"=>14305, "prevInicioStr"=>"06/11/2019"}
{"idprj"=>14305, "prevInicioStr"=>"08/11/2019"}
{"idprj"=>14305, "prevInicioStr"=>"13/01/2020"}

[我想按'prevInicioStr'排序,按'idprj'分组(您可以看到ID 13351没有按应有的方式分组)。为了得到这个结果,我做到了:

result.sort_by { |hash| [ Date.parse(hash['prevInicioStr']), hash['idprj'] ] } 

任何想法?

提前感谢!

ruby sorting hash grouping
1个回答
1
投票

您必须先排序,然后再分组:

© www.soinside.com 2019 - 2024. All rights reserved.