🖥

#ruby で数が大きい順に配列 / ハッシュを並び替えるには reverse が簡単ですよね ( desc sort / sort_by

2019/07/06に公開
[3,1,2].sort.reverse
=> [3, 2, 1]
[{count: 3},{count: 1},{count: 2}].sort_by { |h| h[:count] }.reverse
# => [{:count=>3}, {:count=>2}, {:count=>1}]

別のやり方

[3,1,2].sort { |a, b| b <=> a }
# => [3, 2, 1]
[{count: 3},{count: 1},{count: 2}].sort_by { |h| -h[:count] }
# => [{:count=>3}, {:count=>2}, {:count=>1}]

Original by Github issue

https://github.com/YumaInaura/YumaInaura/issues/2250

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

https://line.me/ti/g2/eEPltQ6Tzh3pYAZV8JXKZqc7PJ6L0rpm573dcQ

Twitter

https://twitter.com/YumaInaura

公開日時

2019-07-06

Discussion