Groovy list.sort by first, second then third elements

前端 未结 7 1259
旧时难觅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:00

    If you want to sort arrays of arbitrary (though homogenous) length, you can use this and it will do it in a single pass:

    def list = [[2, 0, 1], [1, 5, 2], [1, 0, 3]]
    
    list.sort { a, b -> 
        for (int i : (0.. b[i])
            if (comparison) return comparison
        } 
        return 0
    }
    
    assert list == [[1, 0, 3], [1, 5, 2], [2, 0, 1]]
    

提交回复
热议问题