1. 程式人生 > >groovy List 多字段排序

groovy List 多字段排序

object map 字段排序 string compareto col 字段 result ==

List<Map<String, Object>> list = [["loc": "A-02-01","code":"asdf"],["loc": "B-01-01","code":"ff"],
                                  ["loc": "B-01-01","code":"23"], ["loc": "A-01-04","code":"ghg"]]
list.sort{a,b->
    if (a.loc < b.loc) {
        return -1
    }else if (a.loc == b.loc) {
        a.code.compareTo(b.code)
    } 
else { return 1 } }
Result: [[loc:A-01-04, code:ghg], [loc:A-02-01, code:asdf], [loc:B-01-01, code:23], [loc:B-01-01, code:ff]]

groovy List 多字段排序