Friday, 8 September 2017

Java Program to Sort an ArrayList of Custom Objects by Properties Using the Comparator Interface

        In a previous post, we learned about the differences between the Comparable and Comparator interfaces. The Comparator interface is used to define custom sorting logic for objects.

In this post, we will learn how to sort an ArrayList of custom objects based on multiple properties using the Comparator interface.

Example: Sorting an ArrayList of Custom Objects by Multiple Properties

package com.pr;

import java.util.Comparator;
import java.util.Date;

public class ComparatorExample {

     public int id;
     public String name;
     public Date dof;
 
     public ComparatorExample(int id, String name, Date dof) {
            this.id = id;
            this.name = name;
            this.dof = dof;
     }
 
     public int getId() {
           return id;
     }
     public void setId(int id) {
          this.id = id;
     }
     public String getName() {
          return name;
     }
     public void setName(String name) {
          this.name = name;
     }
 
     public Date getDof() {
          return dof;
     }
     public void setDof(Date dof) {
          this.dof = dof;
     }

      //sorting based on Name, ascending order
     public static Comparator<ComparatorExample> nameComp = new Comparator<ComparatorExample> () {
  
            public int compare(ComparatorExample obj1, ComparatorExample obj2) {
                  return (obj1.getName().compareTo(obj2.getName())) ;
            }
     };
 
      //sorting based on id, ascending order
     public static Comparator<ComparatorExample> idComp = new Comparator<ComparatorExample> () {
  
            public int compare(ComparatorExample obj1, ComparatorExample obj2) {
                  if (obj1.getId() > obj2.getId()) {
                        return 1;
                  } else if(obj1.getId() < obj2.getId()) {
                        return -1;
                  } else {
                      return 0;
                  }
            }
      };
 
       // Sorting based on dof(i.e date format), ascending order
      public static Comparator<ComparatorExample> dofComp = new Comparator<ComparatorExample> () {
  
             public int compare(ComparatorExample obj1, ComparatorExample obj2) {
                   return obj1.getDof().compareTo(obj2.getDof());
             }
      };
 
      @Override
      public String toString() {
            return "Name :"+name +", id : "+id+", dof : "+dof;
      }
 }
 
The main class to execute this sorting is,
  

package com.pr;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;

public class MainClassComparator {
      public static void main(String[] args) throws ParseException {
  
            ArrayList<ComparatorExample> list = new ArrayList<ComparatorExample>();
            list.add(new ComparatorExample(60, "Kiran", 
                                  new SimpleDateFormat("yyyy-MM-dd").parse("1988-12-10")));

            list.add(new ComparatorExample(20, "Anand", 
                                   new SimpleDateFormat("yyyy-MM-dd").parse("1987-12-11")));

            list.add(new ComparatorExample(30, "Bharat", 
                                   new SimpleDateFormat("yyyy-MM-dd").parse("1988-12-09")));
  
            //Sorting based on id property
            Collections.sort(list, ComparatorExample.idComp);
            System.out.println("Sorting based on id property");
            for (ComparatorExample e : list) {
                 System.out.println(e);
           }
  
           //Sorting based on name property
            Collections.sort(list, ComparatorExample.nameComp);
            System.out.println("Sorting based on name property");
            for (ComparatorExample e : list) {
                 System.out.println(e);
            }
  
             //Sorting based on dof property
            Collections.sort(list, ComparatorExample.dofComp);
            System.out.println("Sorting based on dof property");
            for (ComparatorExample e : list) {
                 System.out.println(e);
            }
      }
}
OUTPUT
Sorting based on id property
Name :Anand, id : 20, dof : Fri Dec 11 00:00:00 GMT+05:30 1987
Name :Bharat, id : 30, dof : Fri Dec 09 00:00:00 GMT+05:30 1988
Name :Kiran, id : 60, dof : Sat Dec 10 00:00:00 GMT+05:30 1988
 
Sorting based on name property
Name :Anand, id : 20, dof : Fri Dec 11 00:00:00 GMT+05:30 1987
Name :Bharat, id : 30, dof : Fri Dec 09 00:00:00 GMT+05:30 1988
Name :Kiran, id : 60, dof : Sat Dec 10 00:00:00 GMT+05:30 1988
 
Sorting based on dof property
Name :Anand, id : 20, dof : Fri Dec 11 00:00:00 GMT+05:30 1987
Name :Bharat, id : 30, dof : Fri Dec 09 00:00:00 GMT+05:30 1988
Name :Kiran, id : 60, dof : Sat Dec 10 00:00:00 GMT+05:30 1988
 

No comments:

Post a Comment