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...
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…