Generic Comparator in Java

Generic Comparator in JavaGeneric 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.

For Example: List of Objects scenario

Let’s say, we have a Class called PersonVO, it has 6 attributes in it –

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

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?

How to sort multiple columns with Single Comparator based on specific attribute & specific need?

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)

//sorting in ascending order

Collections.sort(persons, new GenericComparator("name", true));

Usage 2

For List of values defined using above defined data types

// 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.

git clone [email protected]:jeevatkm/generic-repo.git

Execution Output of GenericComparator.java

Generic Comparator Execution Result with Sample Person List example
===================================================================

Initial Set of Inputs
*********************
Id	Name	Height	Email Address		DOB		Salary
--	----	------	-------------		---		------
10002	Emily	5.4	[email protected]	1985-02-01	9500
10001	Jacob	5.2	[email protected]	1970-01-01	10500
10004	Chris	5.5	[email protected]	1975-08-01	8500
10003	Andy	5.0	[email protected]	1980-12-01	11500
10005	Jeeva	5.7	[email protected]	1990-03-01	10000

By Id field Sorting (Integer datatype)
****************************************
Id	Name	Height	Email Address		DOB		Salary
--	----	------	-------------		---		------
10001	Jacob	5.2	[email protected]	1970-01-01	10500
10002	Emily	5.4	[email protected]	1985-02-01	9500
10003	Andy	5.0	[email protected]	1980-12-01	11500
10004	Chris	5.5	[email protected]	1975-08-01	8500
10005	Jeeva	5.7	[email protected]	1990-03-01	10000

By Name field Sorting (String datatype)
*****************************************
Id	Name	Height	Email Address		DOB		Salary
--	----	------	-------------		---		------
10003	Andy	5.0	[email protected]	1980-12-01	11500
10004	Chris	5.5	[email protected]	1975-08-01	8500
10002	Emily	5.4	[email protected]	1985-02-01	9500
10001	Jacob	5.2	[email protected]	1970-01-01	10500
10005	Jeeva	5.7	[email protected]	1990-03-01	10000

By Height field Sorting Desc (Float datatype)
*********************************************
Id	Name	Height	Email Address		DOB		Salary
--	----	------	-------------		---		------
10005	Jeeva	5.7	[email protected]	1990-03-01	10000
10004	Chris	5.5	[email protected]	1975-08-01	8500
10002	Emily	5.4	[email protected]	1985-02-01	9500
10001	Jacob	5.2	[email protected]	1970-01-01	10500
10003	Andy	5.0	[email protected]	1980-12-01	11500

By Email Address field Sorting (String datatype)
************************************************
Id	Name	Height	Email Address		DOB		Salary
--	----	------	-------------		---		------
10003	Andy	5.0	[email protected]	1980-12-01	11500
10004	Chris	5.5	[email protected]	1975-08-01	8500
10002	Emily	5.4	[email protected]	1985-02-01	9500
10001	Jacob	5.2	[email protected]	1970-01-01	10500
10005	Jeeva	5.7	[email protected]	1990-03-01	10000

By DOB field Sorting (Date datatype)
************************************
Id	Name	Height	Email Address		DOB		Salary
--	----	------	-------------		---		------
10001	Jacob	5.2	[email protected]	1970-01-01	10500
10004	Chris	5.5	[email protected]	1975-08-01	8500
10003	Andy	5.0	[email protected]	1980-12-01	11500
10002	Emily	5.4	[email protected]	1985-02-01	9500
10005	Jeeva	5.7	[email protected]	1990-03-01	10000

By Salary field Sorting Desc (Long datatype)
********************************************
Id	Name	Height	Email Address		DOB		Salary
--	----	------	-------------		---		------
10003	Andy	5.0	[email protected]	1980-12-01	11500
10001	Jacob	5.2	[email protected]	1970-01-01	10500
10005	Jeeva	5.7	[email protected]	1990-03-01	10000
10002	Emily	5.4	[email protected]	1985-02-01	9500
10004	Chris	5.5	[email protected]	1975-08-01	8500

If you have queries, leave a comment!