LoginSignup
0
0

More than 1 year has passed since last update.

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

Last updated at Posted at 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

チャットメンバー募集

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

Twitter

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0