Groovy list.sort by first, second then third elements

前端 未结 7 1154
旧时难觅i
旧时难觅i 2021-02-02 09:42

I have a groovy list of lists i.e.

list = [[2, 0, 1], [1, 5, 2], [1, 0, 3]]

I would like sort it by order of the first element, then second, th

7条回答
  •  遥遥无期
    2021-02-02 09:56

    Done the Groovy way, regardless of the size of sub-lists:

    ll2.sort { l1, l2 ->
      e1e2 = [l1, l2].transpose().find { e1, e2 ->
          e1 != e2
      }
      e1e2 ? e1e2[0] <=> e1e2[1] : 0
    }
    

提交回复
热议问题