简体   繁体   English

Collections.sort不对我的对象列表进行排序

[英]Collections.sort does not sort my list of objects

I'm trying to sort a list of objects by its id. 我正在尝试按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: 这是一组员工ID的硬编码:

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

Check the results of the employeeId compareTo method. 检查employeeId compareTo方法的结果。

If it always returns 0, the sort will think all the elements are equal. 如果始终返回0,则排序将认为所有元素均相等。 Collections does a stable sort, so if all the elements are equal it will leave the order unchanged. 集合进行稳定的排序,因此,如果所有元素都相等,则顺序将保持不变。

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
}

This is printing correctly output: A1B1 A2B1 A3B1 这是正确打印输出:A1B1 A2B1 A3B1

You are adding the list in the sorted order that's why you are under the impression that your list is not getting the updated. 您正在按排序顺序添加列表,这就是为什么您会觉得列表没有得到更新的原因。 As far as your source is concerned there is no issues you are doing it right. 就您的消息来源而言,您做对的事情没有问题。 The issue is your input. 问题是您的意见。 You are giving the following input in the sorted order as follows. 您将按以下顺序提供以下输入。

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

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

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

Your output will be as below 您的输出如下

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

Try modifying the source as below and check 尝试如下修改源并检查

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

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

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

You will get the result as below 您将得到如下结果

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM