36
32

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 5 years have passed since last update.

Rubyの配列の中のハッシュのソート

Last updated at Posted at 2017-10-19

以前に投稿したPHPの多次元連想配列のソート - QiitaのRuby版をやってみました。
こんな配列の中に入ったハッシュがあったと仮定します。

sort_by.rb
array = [
  {
    :id        => 0,
    :track_num => 7,
    :title     => 'ベィスドロップ・フリークス',
    :artist    => 'かめりあ feat. ななひら'
  },
  {
    :id        => 1,
    :track_num => 1,
    :title     => 'CHRONO DIVER -NORNIR-',
    :artist    =>  'L.E.D.'
  },
  {
    :id        => 2,
    :track_num => 3,
    :title     => 'illuminate',
    :artist    => 'Sota Fujimori'
  },
  {
    :id        => 3,
    :track_num => 10,
    :title     => '表裏一体!?怪盗いいんちょの悩み♥',
    :artist    => '◇◆噂の怪盗少女 ぷらずま★彡Prim◇◆'
  },
  {
    :id        => 4,
    :track_num => 28,
    :title     => 'Night sky',
    :artist    => 'USAO'
  },
  {
    :id        => 5,
    :track_num => 14,
    :title     => 'Night sky (Extended Mix)',
    :artist    => 'USAO'
  },
  {
    :id        => 6,
    :track_num => 30,
    :title     => 'X↑X↓',
    :artist    => 'DJ TECHNORCH'
  },
  {
    :id        => 7,
    :track_num => 11,
    :title     => 'X↑X↓ (Titanium Sisters Long Version)',
    :artist    => 'DJ TECHNORCH'
  },
  {
    :id        => 8,
    :track_num => 31,
    :title     => 'Line 4 Ruin',
    :artist    => 'Ryu☆'
  }
]

これを**track_num(トラックナンバー)**でソートしたい時。
.sort_by で一撃。

sort_by.rb
array.sort_by! { |a| a[:track_num] }
p array

実行結果

$ ruby sort_by.rb
[{:id=>1, :track_num=>1, :title=>"CHRONO DIVER -NORNIR-", :artist=>"L.E.D."}, {:id=>2, :track_num=>3, :title=>"illuminate", :artist=>"Sota Fujimori"}, {:id=>0, :track_num=>7, :title=>"ベィスドロップ・フリークス", :artist=>"かめりあ feat. ななひら"}, {:id=>3, :track_num=>10, :title=>"表裏一体!?怪盗いいんちょの悩み♥", :artist=>"◇◆噂の怪盗少女 ぷらずま★彡Prim◇◆"}, {:id=>7, :track_num=>11, :title=>"X↑X↓ (Titanium Sisters Long Version)", :artist=>"DJ TECHNORCH"}, {:id=>5, :track_num=>14, :title=>"Night sky (Extended Mix)", :artist=>"USAO"}, {:id=>4, :track_num=>28, :title=>"Night sky", :artist=>"USAO"}, {:id=>6, :track_num=>30, :title=>"X↑X↓", :artist=>"DJ TECHNORCH"}, {:id=>8, :track_num=>31, :title=>"Line 4 Ruin", :artist=>"Ryu☆"}]

見辛いなぁ,もう……。
こんな時はppを使用してみましょう。

sort_by.rb
require "pp"

array = [
  {
    :id        => 0,
    :track_num => 7,
    :title     => 'ベィスドロップ・フリークス',
    :artist    => 'かめりあ feat. ななひら'
  },
  {
    :id        => 1,
    :track_num => 1,
    :title     => 'CHRONO DIVER -NORNIR-',
    :artist    =>  'L.E.D.'
  },
  {
    :id        => 2,
    :track_num => 3,
    :title     => 'illuminate',
    :artist    => 'Sota Fujimori'
  },
  {
    :id        => 3,
    :track_num => 10,
    :title     => '表裏一体!?怪盗いいんちょの悩み♥',
    :artist    => '◇◆噂の怪盗少女 ぷらずま★彡Prim◇◆'
  },
  {
    :id        => 4,
    :track_num => 28,
    :title     => 'Night sky',
    :artist    => 'USAO'
  },
  {
    :id        => 5,
    :track_num => 14,
    :title     => 'Night sky (Extended Mix)',
    :artist    => 'USAO'
  },
  {
    :id        => 6,
    :track_num => 30,
    :title     => 'X↑X↓',
    :artist    => 'DJ TECHNORCH'
  },
  {
    :id        => 7,
    :track_num => 11,
    :title     => 'X↑X↓ (Titanium Sisters Long Version)',
    :artist    => 'DJ TECHNORCH'
  },
  {
    :id        => 8,
    :track_num => 31,
    :title     => 'Line 4 Ruin',
    :artist    => 'Ryu☆'
  }
]

array.sort_by! { |a| a[:track_num] }
pp array
$ ruby sort.rb
[{:id=>1, :track_num=>1, :title=>"CHRONO DIVER -NORNIR-", :artist=>"L.E.D."},
 {:id=>2, :track_num=>3, :title=>"illuminate", :artist=>"Sota Fujimori"},
 {:id=>0, :track_num=>7, :title=>"ベィスドロップ・フリークス", :artist=>"かめりあ feat. ななひら"},
 {:id=>3,
  :track_num=>10,
  :title=>"表裏一体!?怪盗いいんちょの悩み♥",
  :artist=>"◇◆噂の怪盗少女 ぷらずま★彡Prim◇◆"},
 {:id=>7,
  :track_num=>11,
  :title=>"X↑X↓ (Titanium Sisters Long Version)",
  :artist=>"DJ TECHNORCH"},
 {:id=>5, :track_num=>14, :title=>"Night sky (Extended Mix)", :artist=>"USAO"},
 {:id=>4, :track_num=>28, :title=>"Night sky", :artist=>"USAO"},
 {:id=>6, :track_num=>30, :title=>"X↑X↓", :artist=>"DJ TECHNORCH"},
 {:id=>8, :track_num=>31, :title=>"Line 4 Ruin", :artist=>"Ryu☆"}]

Yes.

36
32
3

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
36
32

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?