Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有以下哈希:
{"2013-08-12"=> 10, "2013-08-13"=> 20, "2013-11-11"=>30, "2013-11-14"=> 40}
我想要做的是按降序键(格式为yyyy-mm-dd的日期)对其进行排序:
{"2013-11-14"=> 40, "2013-11-11"=>30, "2013-08-13"=> 20, "2013-08-12"=> 10}
这可能吗?
有可能的。
Hash[ {"2013-08-12"=> 10, "2013-08-13"=> 20, "2013-11-11"=>30, "2013-11-14"=> 40} .sort_by{|k, _| k}.reverse ] # => { "2013-11-14" => 40, "2013-11-11" => 30, "2013-08-13" => 20, "2013-08-12" => 10 }