Friday 8 September 2017

Java Program to Sort ArrayList of Custom Objects By Property using Comparator interface

       In previous one post, learned about diff. of  Comparable and Comparator interface.  The Comparator interface is used to sort the two different objects.  In this post or example,  gives to sort the ArrayList of Custom objects with multiple properties.

Example:- Sorting an ArrayList of Custom Objects with 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