Generic Comparator

com.myjeeva.comparator
Class GenericComparator

java.lang.Object
  extended by com.myjeeva.comparator.GenericComparator
All Implemented Interfaces:
Serializable, Comparator

public final class GenericComparator
extends Object
implements Comparator, Serializable

Sorting - Generic Comparator

Author:
Jeevanandam Madanagopal
See Also:
Serialized Form

Constructor Summary
GenericComparator(boolean sortAscending)
          default constructor - assumes comparator for Type List
GenericComparator(String sortField)
          constructor with sortField parameter for Derived type of Class default sorting is ascending order
GenericComparator(String sortField, boolean sortAscending)
          constructor with sortField, sortAscending parameter for Derived type of Class
 
Method Summary
 int compare(Object o1, Object o2)
          
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Comparator
equals
 

Constructor Detail

GenericComparator

public GenericComparator(boolean sortAscending)

default constructor - assumes comparator for Type List

For Example-

List<Integer> aa = new ArrayList<Integer>();
List<String> bb = new ArrayList<String>();
List<Date> cc = new ArrayList<Date>();

and so on..

Invoking sort method with passing GenericComparator for
Collections.sort(aa, new GenericComparator(false));

Parameters:
sortAscending - - a boolean - true ascending order or false descending order

GenericComparator

public GenericComparator(String sortField)

constructor with sortField parameter for Derived type of Class default sorting is ascending order

For Example-

PersonVO person = new PersonVO();
person.setId(10001);
person.setName("Jacob");
person.setHeight(5.2F);
person.setEmailId("[email protected]");
person.setSalary(10500L);
person.setDob(new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse("Jan 1, 1970"));

and person2, person3, so on.. And Defining & adding all the created objects in to below list

List<PersonVO> persons = new ArrayList<PersonVO>();
persons.add(person1);
persons.add(person2);
persons.add(person3);
and so on

Invoking sort method with passing GenericComparator for
Collections.sort(persons, new GenericComparator("name"));

Parameters:
sortField - - a String - which field requires sorting; as per above example "sorting required for name field"

GenericComparator

public GenericComparator(String sortField,
                         boolean sortAscending)

constructor with sortField, sortAscending parameter for Derived type of Class

For Example-

PersonVO person = new PersonVO();
person.setId(10001);
person.setName("Jacob");
person.setHeight(5.2F);
person.setEmailId("[email protected]");
person.setSalary(10500L);
person.setDob(new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse("Jan 1, 1970"));

and person2, person3, so on.. And Defining & adding all the created objects in to below list

List<PersonVO> persons = new ArrayList<PersonVO>();
persons.add(person1);
persons.add(person2);
persons.add(person3);
and so on

Invoking sort method with passing GenericComparator for
Collections.sort(persons, new GenericComparator("name"), false);

Parameters:
sortField - - a String - which field requires sorting; as per above example "sorting required for name field"
sortAscending - - a boolean - true ascending order or false descending order
Method Detail

compare

public int compare(Object o1,
                   Object o2)

Specified by:
compare in interface Comparator

Generic Comparator

Copyright © 2010-2012 www.myjeeva.com, All rights reserved.

The copyright of the pages and contents on this website is with myjeeva.com and the content is licensed under Creative Commons Attribution-Share Alike 3.0 Unported License. Libraries and code snippets on myjeeva.com has license information.