Wednesday, 24 October 2012

Comparator Example



How to compare a bean class and sort it accordingly?

We will use Comparator Interface for this.
  1)      We will create a bean class which will have two properties i.e. name,age. This class will also override the toString method for presentation purpose.
package com.javabreed.example.comparator;

public class PersonBean {
       private String name;
       private int 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;
       }
       @Override
       public String toString() {
              return "PersonBean [name=" + name + ", age=" + age + "]";
       }
      
      
}

  2)      Then we will create a NameComparator class by implementing Comparator interface. This class will have overridden compareTo method to compare the names property of above given bean objects.
package com.javabreed.example.comparator;

import java.util.Comparator;

public class NameComparator implements Comparator<PersonBean> {

       @Override
       public int compare(PersonBean arg0, PersonBean arg1) {
              return arg0.getName().compareTo(arg1.getName());
       }

}

  3)      Similarly we will create a AgeComparator class by implementing Comparator interface. This class will have overridden compare method to compare the age property of above given bean objects.  This comparison will work in ascending order, just flip these ‘>’ or ‘<’ signs to get the results in descending order.
package com.javabreed.example.comparator;

import java.util.Comparator;

public class AgeComparator implements Comparator<PersonBean>{

       public int compare(PersonBean o1, PersonBean o2) {
              int age1=o1.getAge();
       int age2=o2.getAge();
       if(age1 < age2)    // Will give sorted list in ascending order. Flip the '<' to '>' and you can get  
            return -1;     // results in descending order.
        else if(age1 > age2)
            return 1;
        else
            return 0;
       }

}

  4)      Finally a main class to show the results. This class will create a list of PersonBean objects. First it’ll print the unsorted list. Program will then sort this list by name, and then by age.
 package com.javabreed.example.comparator;

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

public class ComparatorMain {
      
       public static void main(String args[]){
              List<PersonBean> list= new ArrayList<PersonBean>();
             
              PersonBean personBean= new PersonBean();  //1st Person
              personBean.setName("Surya");
              personBean.setAge(20);
              list.add(personBean);
             
              personBean= new PersonBean();  //2nd Person
              personBean.setName("Ramya");
              personBean.setAge(25);
              list.add(personBean);
             
              personBean= new PersonBean();  //3rd Person
              personBean.setName("Anil");
              personBean.setAge(23);
              list.add(personBean);
             
              System.out.println("Unsorted List "+list);
              Collections.sort(list,new NameComparator());
              System.out.println("Sorted by name List "+list);
              Collections.sort(list,new AgeComparator());
              System.out.println("Sorted by age List "+list);
       }

}

 Try this and let me know if you have any questions.

No comments:

Post a Comment