Sunday, April 17, 2016

Sorting by type first then alphabetically

The script below ensures that items are sorted by type first then by alphabetical order.

def list = [
[type:"error", value:"yellow"],
[type:"normal", value:"zippy"],
[type:"normal", value:"awesome"],
[type:"error", value:"big"],
]
list.sort { a, b ->
if (a.type.equals("error") != b.type.equals("error")) {
return b.type.equals("error") <=> a.type.equals("error")
} else {
return a.value <=> b.value
}
}
return list​

No comments:

Post a Comment