【Rails】orderメソッドでデータの順番を並び替える方法

orderメソッドとは

データの並び替えをしたい際に使えるのがorderメソッドです。

モデル名.order("並び替えたいカラム名")

#もしくは
モデル名.order(:並び替えたいカラム名)

上記のように並び替えたいカラム名を指定すると、そのカラムのデータがアルファベット順またはあいうえお順に並び替えられます。

また以下のように記述すると、並び替えの順序を逆順にすることができます

モデル名.order("カラム名 DESC")
          or
モデル名.order(カラム名: "DESC")
          or
モデル名.order(カラム名: :DESC)

*DESC = 「降順」ASC = 「昇順」

参考例

[前提]name,age,genderの3つのカラムをもつUserテーブル

カラムageを指定すると、

[1] pry(main)> User.order("age")
  User Load (9.0ms)  SELECT `users`.* FROM `users` ORDER BY age
=> [#<User:0x0000000142680658 id: 2, name: "田中", age: 21, gender: "women", created_at: Fri, 19 Nov 2021 10:15:21.809896000 UTC +00:00, updated_at: Fri, 19 Nov 2021 10:15:21.809896000 UTC +00:00>,
 #<User:0x0000000142680248 id: 8, name: "加藤", age: 21, gender: "women", created_at: Fri, 19 Nov 2021 10:15:21.823235000 UTC +00:00, updated_at: Fri, 19 Nov 2021 10:15:21.823235000 UTC +00:00>,
 #<User:0x0000000113017fc0 id: 11, name: "藤原", age: 21, gender: "men", created_at: Fri, 19 Nov 2021 10:15:21.828604000 UTC +00:00, updated_at: Fri, 19 Nov 2021 10:15:21.828604000 UTC +00:00>,
 #<User:0x0000000113017e58 id: 5, name: "山田", age: 22, gender: "men", created_at: Fri, 19 Nov 2021 10:15:21.815985000 UTC +00:00, updated_at: Fri, 19 Nov 2021 10:15:21.815985000 UTC +00:00>,
 #<User:0x0000000113017d18 id: 1, name: "田中", age: 23, gender: "men", created_at: Fri, 19 Nov 2021 10:15:21.806163000 UTC +00:00, updated_at: Fri, 19 Nov 2021 10:15:21.806163000 UTC +00:00>,
 #<User:0x0000000113017b38 id: 3, name: "佐藤", age: 23, gender: "men", created_at: Fri, 19 Nov 2021 10:15:21.812049000 UTC +00:00, updated_at: Fri, 19 Nov 2021 10:15:21.812049000 UTC +00:00>,
 #<User:0x0000000113017a20 id: 9, name: "宮田", age: 23, gender: "men", created_at: Fri, 19 Nov 2021 10:15:21.824770000 UTC +00:00, updated_at: Fri, 19 Nov 2021 10:15:21.824770000 UTC +00:00>,
 #<User:0x0000000113017890 id: 4, name: "佐藤", age: 24, gender: "women", created_at: Fri, 19 Nov 2021 10:15:21.814096000 UTC +00:00, updated_at: Fri, 19 Nov 2021 10:15:21.814096000 UTC +00:00>,
 #<User:0x00000001130170e8 id: 6, name: "山田", age: 24, gender: "women", created_at: Fri, 19 Nov 2021 10:15:21.818905000 UTC +00:00, updated_at: Fri, 19 Nov 2021 10:15:21.818905000 UTC +00:00>,
 #<User:0x0000000113016ee0 id: 12, name: "中村", age: 28, gender: "women", created_at: Fri, 19 Nov 2021 10:15:21.831359000 UTC +00:00, updated_at: Fri, 19 Nov 2021 10:15:21.831359000 UTC +00:00>,
 #<User:0x0000000113016d78 id: 7, name: "加藤", age: 38, gender: "men", created_at: Fri, 19 Nov 2021 10:15:21.820805000 UTC +00:00, updated_at: Fri, 19 Nov 2021 10:15:21.820805000 UTC +00:00>,
 #<User:0x0000000113016c88 id: 10, name: "荻野", age: 38, gender: "women", created_at: Fri, 19 Nov 2021 10:15:21.826754000 UTC +00:00, updated_at: Fri, 19 Nov 2021 10:15:21.826754000 UTC +00:00>]

21~昇順で若い順に並び替えられました

また、これにDESCを追加すると、

[33] pry(main)> User.order("age DESC")
  User Load (5.5ms)  SELECT `users`.* FROM `users` ORDER BY age DESC
=> [#<User:0x000000011336b7d0 id: 7, name: "加藤", age: 38, gender: "men", created_at: Fri, 19 Nov 2021 10:15:21.820805000 UTC +00:00, updated_at: Fri, 19 Nov 2021 10:15:21.820805000 UTC +00:00>,
 #<User:0x000000011336b690 id: 10, name: "荻野", age: 38, gender: "women", created_at: Fri, 19 Nov 2021 10:15:21.826754000 UTC +00:00, updated_at: Fri, 19 Nov 2021 10:15:21.826754000 UTC +00:00>,
 #<User:0x000000011336b550 id: 12, name: "中村", age: 28, gender: "women", created_at: Fri, 19 Nov 2021 10:15:21.831359000 UTC +00:00, updated_at: Fri, 19 Nov 2021 10:15:21.831359000 UTC +00:00>,
 #<User:0x000000011336b488 id: 4, name: "佐藤", age: 24, gender: "women", created_at: Fri, 19 Nov 2021 10:15:21.814096000 UTC +00:00, updated_at: Fri, 19 Nov 2021 10:15:21.814096000 UTC +00:00>,
 #<User:0x000000011336b3c0 id: 6, name: "山田", age: 24, gender: "women", created_at: Fri, 19 Nov 2021 10:15:21.818905000 UTC +00:00, updated_at: Fri, 19 Nov 2021 10:15:21.818905000 UTC +00:00>,
 #<User:0x000000011336b2f8 id: 1, name: "田中", age: 23, gender: "men", created_at: Fri, 19 Nov 2021 10:15:21.806163000 UTC +00:00, updated_at: Fri, 19 Nov 2021 10:15:21.806163000 UTC +00:00>,
 #<User:0x000000011336b230 id: 3, name: "佐藤", age: 23, gender: "men", created_at: Fri, 19 Nov 2021 10:15:21.812049000 UTC +00:00, updated_at: Fri, 19 Nov 2021 10:15:21.812049000 UTC +00:00>,
 #<User:0x000000011336b168 id: 9, name: "宮田", age: 23, gender: "men", created_at: Fri, 19 Nov 2021 10:15:21.824770000 UTC +00:00, updated_at: Fri, 19 Nov 2021 10:15:21.824770000 UTC +00:00>,
 #<User:0x000000011336b0a0 id: 5, name: "山田", age: 22, gender: "men", created_at: Fri, 19 Nov 2021 10:15:21.815985000 UTC +00:00, updated_at: Fri, 19 Nov 2021 10:15:21.815985000 UTC +00:00>,
 #<User:0x000000011336afd8 id: 2, name: "田中", age: 21, gender: "women", created_at: Fri, 19 Nov 2021 10:15:21.809896000 UTC +00:00, updated_at: Fri, 19 Nov 2021 10:15:21.809896000 UTC +00:00>,
 #<User:0x000000011336af10 id: 8, name: "加藤", age: 21, gender: "women", created_at: Fri, 19 Nov 2021 10:15:21.823235000 UTC +00:00, updated_at: Fri, 19 Nov 2021 10:15:21.823235000 UTC +00:00>,
 #<User:0x000000011336ae48 id: 11, name: "藤原", age: 21, gender: "men", created_at: Fri, 19 Nov 2021 10:15:21.828604000 UTC +00:00, updated_at: Fri, 19 Nov 2021 10:15:21.828604000 UTC +00:00>]

38~逆順で年齢の高いデータから順に並び替えられました

タイトルとURLをコピーしました