LoginSignup
1
0

More than 5 years have passed since last update.

二重配列で複数の条件でソート

Last updated at Posted at 2018-03-08

三項演算子使ってできたのでメモ。

コード

arr = [[1, 2, 3], [2, 3, 4], [2, 4, 4], [2, 4, 5]]
arr.sort!{ |a, b|
  a[0] == b[0] ? (a[1] == b[1] ? b[2] <=> a[2] : b[1] <=> a[1]) : b[0] <=> a[0]
}

arr.each { |item|
    print "#{item[0]} #{item[1]} #{item[2]}\n"
}

結果

2 4 5
2 4 4
2 3 4
1 2 3
1
0
2

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
1
0