使用比较器在java中自定义排序

时间:2016-09-08 10:15:58

标签: java sorting comparator

我有

List<Employee> empList = new ArrayList<Employee>();

        empList.add(new Employee(1, "Arasad", "Paju", "Engineer", 60000.09));
        empList.add(new Employee(23, "Cavi", "Qeja", "Doctor", 8000.89));
        empList.add(new Employee(9, "Darthik", "Sumar", "Surgeon", 2000.23));
        empList.add(new Employee(92, "Bamsi", "Rrishna", "Analyst", 2300.12));
        empList.add(new Employee(12, "Gakesh", "Zotla", "Farmer", 6000.45));

        // Sorting on name and firstname
         Collections.sort(empList,new NameComparator());
        for (Employee e : empList) {
            System.out.println("Sort based on name" + " " + e.getPersno()
                    + " " + e.getName() + " " + e.getFirstname() + " "
                    + e.getFunction() + " " + e.getWage());

        }
    }

我想根据名称和名字进行排序,在我的比较器类中,我已经这样做了

public class NameComparator implements Comparator<Employee> {   

// Customize sorting

@Override
public int compare(Employee e1, Employee e2) {

    return e2.getName().compareToIgnoreCase(e1.getName()) & e2.getFirstname().compareToIgnoreCase(e1.getFirstname());

}

并且在我的输出中没有正确排序

here is my output

Sort based on name&firstname 12 Gakesh Zotla Farmer 6000.45
Sort based on name&firstname 9 Darthik Sumar Surgeon 2000.23
Sort based on name&firstname 23 Cavi Qeja Doctor 8000.89
Sort based on name&firstname 92 Bamsi Rrishna Analyst 2300.12
Sort based on name&firstname  1 Arasad Paju Engineer 60000.09

我的代码有什么问题吗?

2 个答案:

答案 0 :(得分:0)

是:

试试这个:

@Override
public int compare(Employee e1, Employee e2) {
    int result = e1.getName().compareToIgnoreCase(e2.getName()); // reverse order
    if (result == 0) { // their names are equals
        result = e1.getFirstname().compareToIgnoreCase(e2.getFirstname()); // reverse order
    }
    return  result;
}

&是按位运算符,不应将其与&&混淆。

编辑:

打印

Sort based on name&firstname 1 Arasad Paju Engineer 60000.09
Sort based on name&firstname 92 Bamsi Rrishna Analyst 2300.12
Sort based on name&firstname 23 Cavi Qeja Doctor 8000.89
Sort based on name&firstname 9 Darthik Sumar Surgeon 2000.23
Sort based on name&firstname 12 Gakesh Zotla Farmer 6000.45

答案 1 :(得分:0)

这是代码,

class Employee {
    private Integer persno;
    private String name;
    private String FirstName;
    private String function;
    private Double wage;


    public Employee(Integer persno, String name, String firstName,String function, Double wage) {
        super();
        this.persno = persno;
        this.name = name;
        FirstName = firstName;
        this.function = function;
        this.wage = wage;
    }

    public Integer getPersno() {
        return persno;
    }
    public void setPersno(Integer persno) {
        this.persno = persno;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getFirstName() {
        return FirstName;
    }
    public void setFirstName(String firstName) {
        FirstName = firstName;
    }
    public String getFunction() {
        return function;
    }
    public void setFunction(String function) {
        this.function = function;
    }
    public Double getWage() {
        return wage;
    }
    public void setWage(Double wage) {
        this.wage = wage;
    }

    @Override
    public String toString(){
        return this.getPersno()+ " " + this.getName() + " " + this.getFirstName() + " "   + this.getFunction() + " " + this.getWage();
    }

    @Override
    public boolean equals(Object obj){
        if(this.getPersno().equals(((Employee)obj).getPersno()))
                return true;
        else
            return false;
    }
}

class NameComparator implements Comparator<Employee>{

    @Override
    public int compare(Employee o1, Employee o2) {
        return o2.getName().compareToIgnoreCase(o1.getName());
    }
}


List<Employee> empList = new ArrayList<Employee>();

    empList.add(new Employee(1, "Arasad", "Paju", "Engineer", 60000.09));
    empList.add(new Employee(23, "Cavi", "Qeja", "Doctor", 8000.89));
    empList.add(new Employee(9, "Darthik", "Sumar", "Surgeon", 2000.23));
    empList.add(new Employee(92, "Bamsi", "Rrishna", "Analyst", 2300.12));
    empList.add(new Employee(12, "Gakesh", "Zotla", "Farmer", 6000.45));

    Collections.sort(empList, new NameComparator());

    for(Employee emp:empList){
        System.out.println("Sort based on name" + " "+emp.toString());
    }