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 }