Browse Prior Art Database

Custom Generic Comparator to sort any data .

IP.com Disclosure Number: IPCOM000201560D
Publication Date: 2010-Nov-15
Document File: 5 page(s) / 171K

Publishing Venue

The IP.com Prior Art Database

Abstract

This article relates to a custom , generic Java comparator that is capable of sorting any Java data type in any order (ascending or descending ) by simply utilizing the full power of polymorphism.This article also describes how this comparator can be configured with just few lines of code to handle different complex real life data sorting requirements , apart from sorting all possible Java data types.

This text was extracted from a PDF file.
This is the abbreviated version, containing approximately 26% of the total text.

Page 01 of 5

Custom Generic Comparator to sort any data .

Most Java developers are familiar with the

                            ava.util.Comparator interface of the Java API and its capabilities for sorting a collection of objects based on an attribute of the objects in the collection. This works fine when there is only a single field in which sorting is required. But when more and more complex sorting is required , the limitations of sorting on a single field become obvious. What about the situation in which a user needs to sort the data selectively on any field in object collection ?

Generally we write down separate comparators for separate requirements .That is why in most of the Java projects the occurrence of multiple comparators is frequently observed for sorting the data depending on different requirements .Some of them are written for sorting in ascending order , some for descending order , some sort data on string data type ,whereas some sort data on numeric data type . My goal is to write one generic comparator , that will serve all the requirements .

Problem description

Lets describe the problem in a more specific manner.Lets assume that , there is a collection of Person Data Transfer Objects (i.e. a Java class named PersonDTO).Each PersonDTO object present in the data collection is a kind of container of the information of an individual person .Let us assume PersonDTO object contains four fields
name,age ,date of birth and address .

Now if we think there is a data grid which displays details all persons and wants to allow the to sort the person records on any of the mentioned fields . Now we are not going to write four separate comparators for four different fields , we will write one generic comparator that will take care of any number of fields .

To write such a flexible as well as generic comparator ,following are the few challenges :

1.The field name on which sorting needs to be done is dynamic.
2.Sorting order is also dynamic , it can be either ascending or descending.
3.The sorting field data type is not known upfront.Starting from primitive data types to String(like name),Date(like date of birth ) ,numeric (like age )etc , the sorting field can be off any java data type.There is no comparator available which will handle all java data types for sorting .
4.Even if you have a generic comparator that can handle all java data types , still there are some complicated real life sorting requirements ,where even that generic comparator will not be sufficient enough to meet all the requirements .

I am describing such problematic situations,sometimes arising out of the overuse of String for specifying other data types, especially faced by Struts developers.Suppose , a list of DTOs (Data Transfer Object ) needs to be sorted. The DTO has a field named "dateOfBirth" which is of type String but it displays a Date value. The requirement is to sort data on the field "dateOfBirth" . Now ,when the user sorts on "dateOfBirth" field and expects the data to be sorted by date...