sort list of objects by attribute java collections.sort custom comparator java sort list by attribute

The solution for “sort list of objects by attribute java collections.sort custom comparator java sort list by attribute” can be found here. The following code will assist you in solving the problem.

ArrayList employees = getUnsortedEmployeeList();

Comparator compareById = (Employee o1, Employee o2) -> o1.getId().compareTo( o2.getId() );

Collections.sort(employees, compareById);

Collections.sort(employees, compareById.reversed());ArrayList employees = getUnsortedEmployeeList();
             
Comparator compareById = (Employee o1, Employee o2) -> o1.getId().compareTo( o2.getId() );
 
Collections.sort(employees, compareById);
 
Collections.sort(employees, compareById.reversed());
List.sort((a, b) -> {
if (a.value() > b.value()) {
return -1;
}
if (a.value() < b.value()) { return 1; } return 0; });

Thank you for using DeclareCode; We hope you were able to resolve the issue.

More questions on [categories-list]

0
inline scripts encapsulated in