JSF-Richfaces dynamic Sorting with rich:dataTable?

JSF-Richfaces dynamic Sorting with rich:dataTableDynamic sorting using GenericComparator for rich:dataTable, DataModel, SeralizedDataModel, its a key part of every JSF, Richfaces powered applications in the world.  If I say dynamic sorting; of-course you will have couple of thoughts or question in your mind, like this:

  • Dynamic sorting – how it works?
  • Which datatypes are supported in GenericComparator?
  • How to integrate dynamic sorting into JSF and Richfaces component in your application?

This is sorting solution applicable to rich:dataTable, rich:extendedDataTable, rich:rich:scrollableDataTable

Of-course, I will be answering all these questions in detail.  However If you have any queries, please leave a comment.

Dynamic sorting – how it works?

Dynamic sorting is performed using Generic Comparator in Java.  I have created simple and generic comparator class to do this key functionality of your application. Click here to know – how it works.

Datatypes supported in dynamic sorting?

Currently GenericComparator supports following data types.  I’m thinking to include support for Enum type as well in next update.

    • Integer
    • String
    • Long
    • Date
    • Float
    • Double

Integrate dynamic sorting into JSF/Richfaces

Previously I have written an article about How to do: JSF/Richfaces/Seam lazy/dynamic data loading with pagination?.  This article will give a complete idea of rich:dataTable & SerializedDataModel lazy data loading with pagination implementation.  In continuation to that, I will describing below about In which part of the code, you have to put GenericComparator to get a ability of dynamic sorting.

Kindly Note: Assuming you have read above mentioned article and aware of; how it has been implemented and how it has been extended.

Step 1

Health check! Purpose of this check to make you comfortable in below steps, following steps are extension or continuation of above article

  • Understood DynamicDataModel class implementation
  • Understood how SerializableDataModel class extended in above article
  • Understood how walk() method used in the above article

Good work congrats, you’re good to go!

Step 2

Declare following two local variables in DynamicDataModel class to manipulate Sorting values

Step 3

Download a GenericComparator.java from GitHub repo and place it in your project

Step 4

Go to DynamicDataModel class and implement org.richfaces.model.Modifiable interface like below:

Modifiable interface provides one override method called modify(List<FilterField> filterFields, List<SortField2> sortFields); with help of modify method, we are going to determining Key values of sortField & sortOrder (ascending or descending)

As per article we are going to use only second parameter sortFields for sorting; filterFields we will see later in next article.

Step 5

Now key portion of sorting implementation comes to walk() method, that’s why we did a health check in step 1.  You can decide how you want to do implementation of sortField & ascending variable values in two possible way.

Possibility 1

After getting a List values from service/web service using getItemsByrange method you can apply sorting using GenericComparator class

Possibility 2

Passing a sortField & ascending parameter value to getItemsByrange method and process it at service/web service side

Completion view of Possibility 1

Step 6

We have completed the Java portion of sorting implementation, let’s move on to JSF/Richfaces code snippet.  Add following two attributes sortable & sortBy in the rich:column tag, this important to make rich column sortable

Note: sortBy attribute & sorting applicable to only var binding list in the rich:dataTable, rich:extendedDataTable and rich:rich:scrollableDataTable

When user clicks on First Name column on the page, walk() method receives the value which column has clicked and which sort order requested (asc or desc) throught sortField & ascending variable.


  GenericComparator.java   DynamicDataModelWithSorting.java   SampleData.java


That’s all, you have successfully implemented the JSF-Richfaces dynamic Sorting with rich:dataTable!

  • You actually make it seem really easy along with your presentation however I to find this matter to be

    really something which I feel I’d by no means understand. It sort of feels too complicated and extremely huge for me. I am taking a look ahead on your subsequent submit, I will attempt to get the cling of it!

    • Jeevanandam Madanagopal

      Hello – Feel free to post queries queries “which part is not able to understand?” & “which part is too complicated?”; So I can take a look back and update a blog accordingly and then other users will get benefit of it. Looking forward your pointers & suggestions.


  • amlk

    Hi, interface org.richfaces.model.Modifiable seems to be not available in richfaces 4..?

    • jeevatkm

      @amlk Actually it has been refactored in richfaces 4. Those are 
      org.richfaces.model.ArrangeableModel and this model implements org.richfaces.model.Arrangeable Interface
      Javadocs: http://docs.jboss.org/richfaces/latest_4_0_X/javadoc/richfaces-components-ui/org/richfaces/model/ArrangeableModel.html
      PS: currently this article inline with richfaces 3.3.x; thanks for bringing up. I will take out sometime to provide reference and necessary instructions for Richfaces 4 in this article.

  • Shruti

    Hi Jeeva,

    The sorting works perfectly in case of rich:column , however in case of rich:columns

    the modify event is not being called.Can you please provide a solution for rich:columns as well.



    • Hello Shruti – I’m glad, article was helpful to you. I will try to do article around your request. Thanks once again!