Ruby sort by multiple values?

后端 未结 7 919
南旧
南旧 2020-12-07 22:05

I have an array of hashes:

a=[{ \'foo\'=>0,\'bar\'=>1 },
   { \'foo\'=>0,\'bar\'=>2 },
   ... ]

I want to sort the array first

相关标签:
7条回答
  • 2020-12-07 22:39
    a.sort { |a, b| [a['foo'], a['bar']] <=> [b['foo'], b['bar']] }
    
    0 讨论(0)
提交回复
热议问题