I'm trying to sort a list of objects by its id. Here is my code:
List<Employee> employee = getEmployeeList();
Collections.sort(employee, new Comparator<Employee>(){
@Override
public int compare(Employee employee1, Employee employee2) {
return employee1.getEmployeeId().compareTo(employee2.getEmployeeId());
}
});
But nothing happens after the sort, it still shows the original employee list. Am I doing something wrong? I've searched everywhere but they all just do this and it works for them. Here is a hard coded set of Employee Ids:
public class Employee{
private String employeeId = "";
private String employeeName = "";
private String contactNbr = "";
//getters and setters
}
List<Employee> empList = new ArrayList<Employee>();
Employee emp1 = new Employee();
emp1.setEmployeeId("A1B1");
empList.add(emp1);
Employee emp2 = new Employee();
emp2.setEmployeeId("A2B1");
empList.add(emp2);
Employee emp3 = new Employee();
emp3.setEmployeeId("A3B1");
empList.add(emp3);
Collections.sort(empList, new Comparator<Employee>(){
@Override
public int compare(Employee employee1, Employee employee2) {
return employee1.getEmployeeId().compareTo(employee2.getEmployeeId());
}
});