This is my list:
Name: Ben || Age: 5 || Group: 1
Name: Andy || Age: 6 || Group: 2
Name: Charlie || Age: 6 || Group: 2
Name: Ben || Age: 5 || Group: 1
Name: Andy || Age: 5 || Group: 2
Name: Charlie || Age: 5 || Group: 1
I want to sort the list by Group
, if Group
is equal then by Age
, and if Age
is equal then by Name
. But so far I can only sort by one attribute, using Lambda Expressions:
list.sort((Object o1, Object o2) -> o1.getGroup().compareTo(o2.getGroup()));
If I try
o1.getGroup().compareTo(o2.getGroup()) && o1.getAge().compareTo(o2.getAge())
it's turned out error...
Change lambda expression to lambda {block}, and you don't have to specify the parameter types:
You can use the static method
Comparator.comparing
to create a comparator based on a function that returns a comparable value. Such a comparator can be chained with others.Assuming your type is called Person, you would have:
If any of the getters return a primitive type, you have to use - for example -
comparingInt
andthenComparingInt
, respectively. You can also use method references:But ... if your class has a natural ordering according to these values, you better let it implement the interface
Comparable
and write the compare logic in there:This code snippet can also be used in a lambda expression: