Sorting user-defined objects using Java Comparator

In this article, you will see how to sort User-Defined Objects using Java Comparator.

1. Employee Class

Create Employee.java and write the following code.

package com.kruders;
 
public class Employee {
    public String name;
    public int age;
     
    public Employee() {
    }
 
    public Employee(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}

2. Comparator Classes

Now create two classes AgeComparator.java and NameComparator.java that implements Comparator to sort Employees based on age and name respectively.

AgeComparator.java

package com.kruders;
 
import java.util.Comparator;
 
public class AgeComparator implements Comparator<Employee>{
     public int compare(Employee p1, Employee p2) {
          int priority1 = p1.getAge();
          int priority2 = p2.getAge();
 
          if (priority1 == priority2)
                  return 0;
          else if (priority1 > priority2)
                  return 1;
          else
                  return -1;
     }
}

NameComparator.java

package com.kruders;
 
import java.util.Comparator;
 
public class NameComparator implements Comparator<Employee>{
     public int compare(Employee p1, Employee p2) {
         return p1.getName().compareTo(p2.getName());
     }
}

3. Run Program

To sort Employees by age:

Collections.sort(employeeList, new AgeComparator());

To sort Employees by name:

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

In Main.java class, write the following code and run it as Java Application

package com.kruders;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
public class Main {
    public static void main(String args[]) {
        List<Employee> employeeList = new ArrayList<Employee>();
        Employee emp1 = new Employee("Puneet",28);
        Employee emp2 = new Employee("Rajesh",54);
        Employee emp3 = new Employee("Bhuvan",30);
        Employee emp4 = new Employee("Amit",12);
         
        employeeList.add(emp1);
        employeeList.add(emp2);
        employeeList.add(emp3);
        employeeList.add(emp4);
         
        for(Employee emp : employeeList) {
            System.out.println("Name : " + emp.getName() + " , Age : " + emp.getAge());
        }
         
        System.out.println("\nAfter Sorting by Name");
        Collections.sort(employeeList, new NameComparator());
        for(Employee emp : employeeList) {
            System.out.println("Name : " + emp.getName() + " , Age : " + emp.getAge());
        }
         
        System.out.println("\nAfter Sorting by Age");
        Collections.sort(employeeList, new AgeComparator());
        for(Employee emp : employeeList) {
            System.out.println("Name : " + emp.getName() + " , Age : " + emp.getAge());
        }
 
    }
}

The folder structure of the example is shown below in Figure 1.1

Figure 1.1 Figure 1.1

You can download the source code of this example here.


No comments yet.

Leave a Reply