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

以前に投稿した[PHPの多次元連想配列のソート - Qiita](https://qiita.com/shy_azusa/items/54dadc55e3e71cde1445)のRuby版をやってみました。
こんな配列の中に入ったハッシュがあったと仮定します。

```rb: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     => 'Ｘ↑Ｘ↓',
    :artist    => 'DJ TECHNORCH'
  },
  {
    :id        => 7,
    :track_num => 11,
    :title     => 'Ｘ↑Ｘ↓ (Titanium Sisters Long Version)',
    :artist    => 'DJ TECHNORCH'
  },
  {
    :id        => 8,
    :track_num => 31,
    :title     => 'Line 4 Ruin',
    :artist    => 'Ryu☆'
  }
]
```

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

```rb: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=>"Ｘ↑Ｘ↓ (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=>"Ｘ↑Ｘ↓", :artist=>"DJ TECHNORCH"}, {:id=>8, :track_num=>31, :title=>"Line 4 Ruin", :artist=>"Ryu☆"}]
```

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

```rb: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     => 'Ｘ↑Ｘ↓',
    :artist    => 'DJ TECHNORCH'
  },
  {
    :id        => 7,
    :track_num => 11,
    :title     => 'Ｘ↑Ｘ↓ (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=>"Ｘ↑Ｘ↓ (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=>"Ｘ↑Ｘ↓", :artist=>"DJ TECHNORCH"},
 {:id=>8, :track_num=>31, :title=>"Line 4 Ruin", :artist=>"Ryu☆"}]
```

Yes.