Groovy list.sort by first, second then third elements

前端 未结 7 1241
旧时难觅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 10:07

    Here's what I came up with, not the most groovy way I suppose..

    list = list.sort{ a,b -> 
        if(a[0].compareTo(b[0]) == 0) {
            if(a[1].compareTo(b[1]) == 0) {
                return a[2].compareTo(b[2]);
            } else {
                return a[1].compareTo(b[1]);
            }
        } else {
            return a[0].compareTo(b[0]);
        }
    }
    

提交回复
热议问题