🖥
#ruby で数が大きい順に配列 / ハッシュを並び替えるには reverse が簡単ですよね ( desc sort / sort_by
[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
チャットメンバー募集
何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。
公開日時
2019-07-06
Discussion