LoginSignup
2
3

More than 3 years have passed since last update.

Rubyでsortとeachを組み合わせる

Posted at

eventsというeventインスタンスの集合があったとします。

eventsに存在するeventのidを一覧表示させるためには以下のようにします。

$ events.each {|event| puts event.id }

12359
12138
7184
6872
13547
5582

表示されたidをソート順で確認したい場合は以下のようにします。

$ events.sort_by {|event| event.id }.each {|event| puts event.id }

5582
6872
7184
12138
12359
13547
2
3
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
2
3