0

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());
    }
});
4

3 回答 3

0

检查employeeId compareTo 方法的结果。

如果它总是返回 0,则排序会认为所有元素都是相等的。集合进行稳定排序,因此如果所有元素都相等,它将保持顺序不变。

于 2012-12-21T02:26:42.453 回答
0

您正在按排序顺序添加列表,这就是为什么您认为您的列表没有得到更新的印象。就您的消息来源而言,您做对了没有任何问题。问题是您的输入。您按如下排序顺序提供以下输入。

emp1.setEmployeeId("A1B1");
empList.add(emp1);

emp1.setEmployeeId("A2B1");
empList.add(emp2);

emp1.setEmployeeId("A3B1");
empList.add(emp3);

您的输出将如下所示

Before sorting: [A1B1, A2B1, A3B1]
After sorting:  [A1B1, A2B1, A3B1]

尝试如下修改源并检查

emp1.setEmployeeId("A3B1");
empList.add(emp3);

emp1.setEmployeeId("A2B1");
empList.add(emp2);

emp1.setEmployeeId("A1B1");
empList.add(emp1);

你会得到如下结果

Before sorting: [A3B1, A2B1, A1B1]
After sorting:  [A1B1, A2B1, A3B1]
于 2012-12-21T05:34:07.497 回答
0
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

public class MainTest {

    public static void main(String[] arg)
    {
        List<Employee> empList = new ArrayList<Employee>();
        Employee emp1 = new Employee();
        emp1.setEmployeeId("A2B1");
        empList.add(emp1);

        Employee emp2 = new Employee();
        emp2.setEmployeeId("A1B1");
        empList.add(emp2);

        Employee emp3 = new Employee();
        emp3.setEmployeeId("A3B1");
        empList.add(emp3);

        Collections.sort(empList, new Comparator<Employee>(){

            public int compare(Employee employee1, Employee employee2) {
                return employee1.getEmployeeId().compareTo(employee2.getEmployeeId());
            }
        });


        Iterator i = empList.iterator();
        while(i.hasNext()){
            System.out.println(((Employee)i.next()).getEmployeeId());
        }


    }
}
class Employee{
    private String employeeId = "";
    private String employeeName = "";
    private String contactNbr = "";
    public String getContactNbr() {
        return contactNbr;
    }
    public void setContactNbr(String contactNbr) {
        this.contactNbr = contactNbr;
    }
    public String getEmployeeId() {
        return employeeId;
    }
    public void setEmployeeId(String employeeId) {
        this.employeeId = employeeId;
    }
    public String getEmployeeName() {
        return employeeName;
    }
    public void setEmployeeName(String employeeName) {
        this.employeeName = employeeName;
    }

    //getters and setters
}

这是正确打印输出:A1B1 A2B1 A3B1

于 2012-12-21T04:37:29.567 回答