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] sortメソッド

Posted at

##はじめに
今回はsortメソッドという配列の順番を並び替えられるメソッドを紹介します。

##sortメソッド
配列に対して使います。

配列に対してsortメソッドを使うと中身を昇順に並び替えてくれます。

array = [1, 5, 3, 2, 4]
array.sort
=> [1, 2, 3, 4, 5]

またreverseメソッドを組み合わせれば降順にできます。

array.sort.reverse
=> [5, 4, 3, 2, 1]

文字列にも使えます。

array = ["b", "a", "d", "f", "e", "c"]
array.sort
=> ["a", "b", "c", "d", "e", "f"]

sort! とすることで破壊的に使用することも可能です。

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?