0 votes
2.8k views
in Java by
Sort a List of objects by multiple fields
Is it possible to sort the list according to multiple fields? Which one is possible to use Comparator or the Comparable interface ?

1 Answer

0 votes
by

You see the following example, to sort list of employees using Comparator interface. Comparator interface is exist in java.uitl package.

Main Class:
package test.com.collection.comparator;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import test.com.collection.Employee;

public class TestCompatator {

    public static void main(String[] args) {
        List<Employee> employees = getEmployees();
        System.out.println("################## Without sorting Employee List ######################");
        for(Employee emp: employees){
            System.out.println("Id: " + emp.getId() +" FirstName: " + emp.getFirstName() + " LastName: " + emp.getLastName() + " EmpCode: " + emp.getCode() + " EmpSalary: " + emp.getSalary());
        }
        EmployeeSort employeeSort = new EmployeeSort();
        Collections.sort(employees, employeeSort);
        System.out.println("################## After sorting Employee List ######################");
        for(Employee emp: employees){
            System.out.println("Id: " + emp.getId() +" FirstName: " + emp.getFirstName() + " LastName: " + emp.getLastName() + " EmpCode: " + emp.getCode() + " EmpSalary: " + emp.getSalary());
        }
    }

    private static List<Employee> getEmployees() {
        List<Employee> emps = new ArrayList<Employee>();
        Employee e1 = new Employee(1, "Aman", "Raj", "E001", 100000D);
        Employee e2 = new Employee(2, "Birendra", "Rai", "E002", 1500D);
        Employee e3 = new Employee(3, "Bipin", "Kumar", "E003", 2005D);
        Employee e4 = new Employee(4, "Suraj", "Kumar", "E004", 45004D);
        Employee e5 = new Employee(5, "Suraj", "Raj", "E005", 46800D);
        Employee e6 = new Employee(6, "Suraj", "Raj", "E006", 44800D);
        
        emps.add(e1);
        emps.add(e2);
        emps.add(e3);
        emps.add(e4);
        emps.add(e5);
        emps.add(e6);
        
        return emps;
    }
}

Sorter Class:
package test.com.collection.comparator;

import java.util.Comparator;

import test.com.collection.Employee;


public class EmployeeSort implements Comparator<Employee>{

    public int compare(Employee e1, Employee e2) {
        int num = e1.getFirstName().compareTo(e2.getFirstName());
        if(num ==0){
            num = e1.getLastName().compareTo(e2.getLastName());
        }
        if(num ==0){
            num = e1.getSalary().compareTo(e2.getSalary());
        }
        return num;
    }
}

Bean Class:
package test.com.collection;

public class Employee {
    private Integer id;
    private String firstName;
    private String lastName;
    private String code;
    private Double salary;
    
    public Employee(Integer id, String firstName, String lastName, String code, Double salary){
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.code = code;
        this.salary = salary;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getFirstName() {
        return firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public String getCode() {
        return code;
    }
    public Double getSalary() {
        return salary;
    }
}

Output:
################## Without sorting Employee List ######################
Id: 1 FirstName: Aman LastName: Raj EmpCode: E001 EmpSalary: 100000.0
Id: 2 FirstName: Birendra LastName: Rai EmpCode: E002 EmpSalary: 1500.0
Id: 3 FirstName: Bipin LastName: Kumar EmpCode: E003 EmpSalary: 2005.0
Id: 4 FirstName: Suraj LastName: Kumar EmpCode: E004 EmpSalary: 45004.0
Id: 5 FirstName: Suraj LastName: Raj EmpCode: E005 EmpSalary: 46800.0
Id: 6 FirstName: Suraj LastName: Raj EmpCode: E006 EmpSalary: 44800.0
################## After sorting Employee List ######################
Id: 1 FirstName: Aman LastName: Raj EmpCode: E001 EmpSalary: 100000.0
Id: 3 FirstName: Bipin LastName: Kumar EmpCode: E003 EmpSalary: 2005.0
Id: 2 FirstName: Birendra LastName: Rai EmpCode: E002 EmpSalary: 1500.0
Id: 4 FirstName: Suraj LastName: Kumar EmpCode: E004 EmpSalary: 45004.0
Id: 6 FirstName: Suraj LastName: Raj EmpCode: E006 EmpSalary: 44800.0
Id: 5 FirstName: Suraj LastName: Raj EmpCode: E005 EmpSalary: 46800.0
 

Share:- Whatsapp Facebook Facebook


Welcome to Developerhelpway Q&A, where you can ask questions and receive answers from other members of the community.

Categories

...