Wednesday, 3 April 2013

Sorting the list with dynamically generated comparator

Let me start the blog again with the all important statistics. Inheriting a stat from my previous blog, 72% of the java programmed applications in the world are web applications. Among which, 61% present the information in tabular form. Among which, 79.3% provide the facilities for sorting the data by different columns. Among which, 91% applications have different logic for different type of sorting.  Not to mention that these statistics are as important as Ajit Agarkar’s place in Indian cricket team.

Anyways, coming to the point now, have a look at the table below. It shows the name of Employees along with the other details (the names sound familiar, don’t they?). Imagine that you have the similar table which is being displayed on the web page and you have to provide the functionality to sort these in ascending/descending order of any column (depending upon user selection). We can do this in two ways, by client side scripting (javascript/jQuery) or by server side sorting (java sorting). We will discuss the latter scenario in this blog.





Suppose you are strictly advised not to write any scripting code in your page (as the client may disable scripting in the browser) and the person who reviews your code is a homicidal maniac and he knows where you live! In this case, you have no other option but to write server side sorting mechanism. You won’t risk your life, would you?

Talking about Java, it provides built in sorting mechanism for sorting the lists (sort method of Collections class). But what if the list consists of objects of user defined class? Yes, you guessed it right. We have to write the comparison logic using comparable/comparator interface. Now, in our case, we have to write multiple sorting mechanisms so using comparable definitely won’t help, hence, we need to write different comparators for different sorting mechanisms. Imagine how tedious it would be to write the different comparators for different sorting mechanisms? And what if tomorrow, another field or two get added into that table? We have to write the new comparators.

The approach which I am talking about is, why not generate the comparator dynamically depending upon the requirement? How awesome would that be if you have the method which takes arguments like class name, field name and sorting order and gives you the comparator which you can directly use! This is exactly what I have created.

When we make a class implement comparator, we generally override the ‘compare’ method which returns an integer depending upon the comparison. In the definition of ‘compare’ method, we generally call ‘compareTo’ method of respective field which comparator is created for (e.g. String1.compareTo(String2), Date1.compareTo(Date2) etc).  The steps which my method performs are mentioned in the table below. Please note that the method has 3 arguments: Class Name, Attribute Name and Sort order.



The code along with an example is placed at the end of the blogYou can download/modify the code according to the requirements. Following are the prerequisites for the code to run successfully.
  1. In the first argument of the method (class name), you must provide the fully qualified class name of the class whose instances you want to compare, otherwise you will get a big fat ClassNotFoundException. In the example, I haven’t used the fully qualified class name because of the class is in the same package.
  2. The Class whose objects need to be sorted must adhere to Java bean naming standards. (i.e. all the instance variables should be private and should have getters and setters).
  3. The comparison works for all the primitive types. As mentioned in the algorithm, the method calls ‘compareTo’ method of respective object type. As all the wrapper classes have ‘compareTo’ method defined, comparison is possible. However, if your bean class has the reference of other class and you want to sort according to that reference then, you must implement ‘compareTo’ method in that class and write your comparison logic into it.
The example shows different sorting mechanisms for different fields. I have provided the comments in the code wherever possible to make the code understandable.

Please have your thoughts in the comments section.

The code is placed here.

Happy comparing!