|
Generic Comparator | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.myjeeva.comparator.GenericComparator
public final class GenericComparator
Sorting - Generic Comparator
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 |
---|
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
for GenericComparator
Collections.sort(aa, new GenericComparator(false));
sortAscending
- - a boolean
- true
ascending order or false
descending orderpublic 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>();
and so on
persons.add(person1);
persons.add(person2);
persons.add(person3);
Invoking sort method with passing
for GenericComparator
Collections.sort(persons, new GenericComparator("name"));
sortField
- - a String
- which field requires sorting; as per above example "sorting required for name
field"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>();
and so on
persons.add(person1);
persons.add(person2);
persons.add(person3);
Invoking sort method with passing
for GenericComparator
Collections.sort(persons, new GenericComparator("name"), false);
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 orderMethod Detail |
---|
public int compare(Object o1, Object o2)
compare
in interface Comparator
|
Generic Comparator | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |