0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Ruby】配列(数値)を大小で並び替えて◯番目を抽出する

Posted at

やったこと

i = [4, 18, 25, 20, 9, 13] #変数に配列を代入
p = i.sort.reverse #.sortはデフォルトで昇順(小→大)のため、.reverseで降順(大→小)に変更
puts p[2] #3番目に大きいものを表示

.reverseで降順にすることで、
配列の数に関わらず、大きいものから3番目が抽出できます。

0
0
0

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?