LoginSignup
32

More than 5 years have passed since last update.

posted at

updated at

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

以前に投稿した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.

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
What you can do with signing up
32