October 22, 2021

Sort Employee Object Saved in ArrayList Based On Condition

Few employee objects are saved in arraylist. Will try to sort these element either by Employee name or Employee salary. I have created 2 java files: Employee.java, SortEmployee.java in same package.

package com.chetasmind.listPackage;

public class Employee {
    
    private int empID;
    private String empName;
    private double empSalary;
     
    public Employee() {
         
    }
    public Employee(int empID,String empName,double empSalary) {
        this.empID = empID;
        this.empName = empName;
        this.empSalary = empSalary;
    }
     
    public int getEmpID() {
        return empID;
    }
    public void setEmpID(int empID) {
        this.empID = empID;
    }
    public String getEmpName() {
        return empName;
    }
    public void setEmpName(String empName) {
        this.empName = empName;
    }
    public double getEmpSalary() {
        return empSalary;
    }
    public void setEmpSalary(double empSalary) {
        this.empSalary = empSalary;
    }
     
    @Override
    public String toString() {
         
        String empDetails = "Employee ID: "+this.empID+" Employee Name: "+this.empName+" Salary="+this.empSalary; 
        return empDetails;
         
    }
}
package com.chetasmind.listPackage;

import java.util.ArrayList;
import java.util.Comparator;
 
class SalaryComparator implements Comparator<Employee>{
 
    @Override
    public int compare(Employee o1, Employee o2) {
         
        if(o1.getEmpSalary() > o2.getEmpSalary())
            return 1;
        else if(o1.getEmpSalary() < o2.getEmpSalary())
            return -1;
        else
            return 0;
    }
}
class NameComparator implements Comparator<Employee>{
 
    @Override
    public int compare(Employee o1, Employee o2) {
        return o1.getEmpName().compareTo(o2.getEmpName());
    }
}
 
public class SortEmployee {
 
    public static void main(String[] args) {
         
        ArrayList<Employee>obj = new ArrayList<>();
         
        Employee emp1 = new Employee(101,"Sagar",45000);
        Employee emp2 = new Employee(102,"Rekha",85000);
        Employee emp3 = new Employee(103,"Suhas",35000);
        Employee emp4 = new Employee(104,"Kavya",80000);
          
        obj.add(emp1);
        obj.add(emp2);
        obj.add(emp3);
        obj.add(emp4);
         
        System.out.println("Displaying Arraylist element before sorting");
        for(int i=0;i<obj.size();i++) {
            System.out.println(obj.get(i));
        }
         
        String compareUsing = "salary";
         
        if(compareUsing.equals("salary"))
            obj.sort(new SalaryComparator());
        else
            obj.sort(new NameComparator());
         
        System.out.println("Displaying Arraylist element after sorting");
        for(int i=0;i<obj.size();i++) {
            System.out.println(obj.get(i));
        }
    }
}

SalaryComparator class implements Comparator interface. So, you must write compare method. This compare method accepts 2 Employee object: Employee o1, Employee o2. This method compares these object based on salary and passes the integer value. Same case with NameComparator class. This compares the employee name.

In line no: 48, i have created a String literal compareUsing = “salary”; You can change this value to compare using name. Output of above code is as shown below.

Displaying Arraylist element before sorting
Employee ID: 101 Employee Name: Sagar Salary=45000.0
Employee ID: 102 Employee Name: Rekha Salary=85000.0
Employee ID: 103 Employee Name: Suhas Salary=35000.0
Employee ID: 104 Employee Name: Kavya Salary=80000.0
Displaying Arraylist element after sorting
Employee ID: 103 Employee Name: Suhas Salary=35000.0
Employee ID: 101 Employee Name: Sagar Salary=45000.0
Employee ID: 104 Employee Name: Kavya Salary=80000.0
Employee ID: 102 Employee Name: Rekha Salary=85000.0

The above application is also present in my github repository.
If you are writing java applications for the first time, refer my Project structure article.

Leave a Reply

Your email address will not be published. Required fields are marked *