Wednesday, 19 November 2014

Sort List of objects on multiple fields


This is one of my favorite interview questions on java collections. Let’s say there is Employee object with fields like firstName, lastName and salary. Now I want to sort list of employees based on firstName and Salary.

firstName
lastName
salary
Hari
Krishna
25000
Hari
Kishan
50000
Piyush
Chawla
65000
Shreyas
Desai
150000
Maruthi
Kumar
75000
Maruthi
Krishna
35000

We can solve this problem by writing custom comparator. First we have to compare employee objects using their firstName. If their firstNames are equal, then we have to compare again using salaries and return the value. If firstNames are different, then no need to compare employee objects further.
Compare function code looks like below.

public int compare(Employee o1, Employee o2) {
        String firstName1 = o1.getFirstName();
        String firstName2 = o2.getFirstName();

        /* If names are equal compare by using salaries */
        if(firstName1.compareTo(firstName2) == 0){
            Double salary1 = o1.getSalary();
            Double salary2 = o2.getSalary();
            return salary1.compareTo(salary2);
        }

        return firstName1.compareTo(firstName2);
}


Employee.java
public class Employee{
    private String firstName;
    private String lastName;
    private double salary;

    public Employee(String firstName, String lastName, double salary){
        this.firstName = firstName;
        this.lastName = lastName;
        this.salary = salary;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public double getSalary() {
        return salary;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }
}

EmployeeComparator.java
import java.util.*;

public class EmployeeComparator implements Comparator<Employee>{

    public int compare(Employee o1, Employee o2) {
        String firstName1 = o1.getFirstName();
        String firstName2 = o2.getFirstName();

        /* If names are equal compare by using salaries */
        if(firstName1.compareTo(firstName2) == 0){
            Double salary1 = o1.getSalary();
            Double salary2 = o2.getSalary();
            return salary1.compareTo(salary2);
        }

        return firstName1.compareTo(firstName2);
    }

}


TestApplication.java
import java.util.*;

public class TestApplication {
    public static void main(String args[]){
        Employee e1 = new Employee("Hari", "Krishna", 25000);
        Employee e2 = new Employee("Hari", "Kishan", 50000);
        Employee e3 = new Employee("Piyush", "Chawla", 65000);
        Employee e4 = new Employee("Shreyas", "Desai", 150000);
        Employee e5 = new Employee("Maruthi", "Kumar", 75000);
        Employee e6 = new Employee("Maruthi", "Krishna", 35000);
        Employee emp;

        List<Employee> empList = new ArrayList<Employee> ();
        empList.add(e1);
        empList.add(e2);
        empList.add(e3);
        empList.add(e4);
        empList.add(e5);
        empList.add(e6);

        Collections.sort(empList, new EmployeeComparator());
        Iterator<Employee> iter = empList.iterator();

        while(iter.hasNext()){
            emp = iter.next();
            System.out.println(emp.getFirstName() +" " + emp.getLastName() +" " +emp.getSalary());
        }
        
    }
}


Output
Hari Krishna 25000.0
Hari Kishan 50000.0
Maruthi Krishna 35000.0
Maruthi Kumar 75000.0
Piyush Chawla 65000.0
Shreyas Desai 150000.0


Related Links

No comments:

Post a Comment