java sort list by attribute code example
Example 1: sort list of objects by attribute java
ArrayList employees = getUnsortedEmployeeList();
Comparator compareById = (Employee o1, Employee o2) -> o1.getId().compareTo( o2.getId() );
Collections.sort(employees, compareById);
Collections.sort(employees, compareById.reversed());
Example 2: collections.sort custom comparator
ArrayList employees = getUnsortedEmployeeList();
Comparator compareById = (Employee o1, Employee o2) -> o1.getId().compareTo( o2.getId() );
Collections.sort(employees, compareById);
Collections.sort(employees, compareById.reversed());