Generic Comparator in Java Collections, from word itself describes the purpose of it. I ran into the need of one generic comparator which I could use for storing POJO in the List and developed the generic comparator Class which could be used with Collections.sort() method in Java; to sort a List of values in ascending or descending order. This class support following data types Integer, String, Long, Double, Float, Date in the POJO can be sort effectively.
Let’s say, we have a Class called PersonVO, it has 6 attributes in it –
1 2 3 4 5 6 |
private Integer id; private String name; private Float height; private Long salary; private String emailId; private Date dob; |
And using above PersonVO Class defining List a objects
1 |
List<PersonVO> persons = new ArrayList<PersonVO>(); |
In typical scenario you may have to write multiple comparator class for each data type and based attribute you have to supply specific comparator to achieve sorting for each column in your Application. Now, Question for you?
Here it comes answer to above question; make use of GenericComparator.java in your application to achieve sorting need dynamically. I’m going to describe, how to use GenericComparator.java and execution with sample output.
How to use: GenericComparator.java?
Download GenericComparator.java from GitHub repo and place the Java file into your project. Now you can take advantage in two ways:
Usage 1
For List for objects and sorting underneath object attribute value (for e.g. persons list object)
1 2 3 |
//sorting in ascending order Collections.sort(persons, new GenericComparator("name", true)); |
Usage 2
For List of values defined using above defined data types
1 2 3 |
// sorting in descending order Collections.sort(persons, new GenericComparator(false)); |
That’s all, its easy to use ‘Generic Comparator in Java’!
Docs & Downloads
Javadoc: GenericComparator.java and Sample Demo Project genericComparator you can git clone from GitHub.
1 |
git clone git@github.com:jeevatkm/generic-repo.git |
Execution Output of GenericComparator.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
Generic Comparator Execution Result with Sample Person List example =================================================================== Initial Set of Inputs ********************* Id Name Height Email Address DOB Salary -- ---- ------ ------------- --- ------ 10002 Emily 5.4 emily@example.example 1985-02-01 9500 10001 Jacob 5.2 jacob@example.example 1970-01-01 10500 10004 Chris 5.5 chris@example.example 1975-08-01 8500 10003 Andy 5.0 andy@example.example 1980-12-01 11500 10005 Jeeva 5.7 sample@example.example 1990-03-01 10000 By Id field Sorting (Integer datatype) **************************************** Id Name Height Email Address DOB Salary -- ---- ------ ------------- --- ------ 10001 Jacob 5.2 jacob@example.example 1970-01-01 10500 10002 Emily 5.4 emily@example.example 1985-02-01 9500 10003 Andy 5.0 andy@example.example 1980-12-01 11500 10004 Chris 5.5 chris@example.example 1975-08-01 8500 10005 Jeeva 5.7 sample@example.example 1990-03-01 10000 By Name field Sorting (String datatype) ***************************************** Id Name Height Email Address DOB Salary -- ---- ------ ------------- --- ------ 10003 Andy 5.0 andy@example.example 1980-12-01 11500 10004 Chris 5.5 chris@example.example 1975-08-01 8500 10002 Emily 5.4 emily@example.example 1985-02-01 9500 10001 Jacob 5.2 jacob@example.example 1970-01-01 10500 10005 Jeeva 5.7 sample@example.example 1990-03-01 10000 By Height field Sorting Desc (Float datatype) ********************************************* Id Name Height Email Address DOB Salary -- ---- ------ ------------- --- ------ 10005 Jeeva 5.7 sample@example.example 1990-03-01 10000 10004 Chris 5.5 chris@example.example 1975-08-01 8500 10002 Emily 5.4 emily@example.example 1985-02-01 9500 10001 Jacob 5.2 jacob@example.example 1970-01-01 10500 10003 Andy 5.0 andy@example.example 1980-12-01 11500 By Email Address field Sorting (String datatype) ************************************************ Id Name Height Email Address DOB Salary -- ---- ------ ------------- --- ------ 10003 Andy 5.0 andy@example.example 1980-12-01 11500 10004 Chris 5.5 chris@example.example 1975-08-01 8500 10002 Emily 5.4 emily@example.example 1985-02-01 9500 10001 Jacob 5.2 jacob@example.example 1970-01-01 10500 10005 Jeeva 5.7 sample@example.example 1990-03-01 10000 By DOB field Sorting (Date datatype) ************************************ Id Name Height Email Address DOB Salary -- ---- ------ ------------- --- ------ 10001 Jacob 5.2 jacob@example.example 1970-01-01 10500 10004 Chris 5.5 chris@example.example 1975-08-01 8500 10003 Andy 5.0 andy@example.example 1980-12-01 11500 10002 Emily 5.4 emily@example.example 1985-02-01 9500 10005 Jeeva 5.7 sample@example.example 1990-03-01 10000 By Salary field Sorting Desc (Long datatype) ******************************************** Id Name Height Email Address DOB Salary -- ---- ------ ------------- --- ------ 10003 Andy 5.0 andy@example.example 1980-12-01 11500 10001 Jacob 5.2 jacob@example.example 1970-01-01 10500 10005 Jeeva 5.7 sample@example.example 1990-03-01 10000 10002 Emily 5.4 emily@example.example 1985-02-01 9500 10004 Chris 5.5 chris@example.example 1975-08-01 8500 |
If you have queries, leave a comment!