简体   繁体   中英

Customized sorting in java using comparator

I have

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

        }
    }

i want to sort based on name and firstname and in my comparator class i have done like this

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

}

and in my output it is not getting sorted properly

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

is there anything wrong in my code?

Yes:

Try with this:

@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;
}

& is bitwise operator, you should not confuse it with && .

EDITED:

It prints

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

Here is the code,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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