With latest version of groovy You can achieve this with @Sortable annotation.
Second option is to implement Comparable
and use spaceship
(<=>
) operator.
And here's sample just with sort
method:
import groovy.transform.ToString
@ToString
class R {
def f
def l
}
def list = [new R(f: 'A', l: 'B'),new R(f: 'A', l: 'A'),new R(f: 'A', l: 'C'),new R(f: 'B', l: 'C')]
list.sort {left, right -> left.l <=> right.l ?: left.f <=> right.f}