Reputation: 1378
I'm need to make sure i understand correctly someone else code at work this block sort this object: theObjectList by the variable getId() what do i need to do in order to add another variable to the sorting ? for example getName()
protected void fillData(List<AnyObject> theObjectList) {
Collections.sort(theObjectList, (A, B) -> A.getId() - B.getId());
/* more code */
}
Upvotes: 0
Views: 55
Reputation: 2608
Believe you'd need to understand and pickup lambda expressions. They have been introduced since jdk8.
Upvotes: 1
Reputation: 533442
You can use Comparators.comparing
Collections.sort(theObjectList,
Comparator.comparing(x -> x.getId()).andThen(x -> x.getName()));
Upvotes: 3