4 Replies Latest reply on Apr 16, 2013 4:47 AM by Bratislav Vukojicic

    Richfaces 3. How to custom sort in columns always displaying null values at the end

    siva999 Newbie

      Hi All,

       

      I'm using <rich:dataTable/> with some <rich:column/> columns.

      The data I want to display contains some null values on a property.

      Basically, the sort feature of <rich:column/> is to display those null values on the top or bottom of the list, depending on the current sort direction value.

      I want to display those null values always on the bottom of the list.

      Can I use a Comparator for sorting a column? If so ... are there any examples somewhere or more detailed documentation ?

       

      Regards

        • 1. Richfaces 3. How to custom sort in columns always displaying null values at the end
          Ilya Shaikovsky Master

          use comparator attribute. define your own comparator which will works as you need.

          • 2. Richfaces 3. How to custom sort in columns always displaying null values at the end
            siva999 Newbie

            Thanks Ilya. To test I tried to use my own comparator, a simple one to sort the dates in ascending order but it is not working. I am not sure what i am doing wrong here.

             

            <rich:dataTable id="tableId" var="_cmpts" value="#{cmptBean.cmptList}">

            <rich:column id="cpmtName"   sortBy="#{_cmpts.name}"    sortable="true">
                <f:facet name="header">   <h:outputText value="Name"/>  </f:facet>
                <h:outputText value="#{_cmpts.name}" />
            </rich:column>
              

            <rich:column id="startDate" sortBy="#{_cmpts.startDate}"  sortable="true"  sortOrder="#{sortBean.startDateOrder}"      

                  comparator="#{sortBean.startDateComparator}">
               <f:facet name="header"> <h:outputText value="Start Date"/> </f:facet>
               <h:outputText value="#{cmpts.startDate}" />
            </rich:column>

            </rich:dataTable>

             

            public class SortBean implements Serializable {
               
                 private static final long serialVersionUID = 1L;
                 public SortBean() {
                 }
                
                 private Ordering startDateOrder = Ordering.ASCENDING;
                
                 public Ordering getStartDateOrder() {
                  return startDateOrder;
                 }
               
                 public void setStartDateOrder(Ordering startDateOrder) {
                  this.startDateOrder = startDateOrder;
                 }
               
                 public Comparator<Cmpt> getStartDateComparator(){
                  return new Comparator<Cmpt>() {
                         public int compare(Cmpt c1, Cmpt c2) {

                          return c1.getStartDate().compareTo(c2.getStartDate());
                         }
                  };
                 }
                 
            }

             

            When i debug i can observe that getStartDateComparator() is being called.

            • 3. Re: Richfaces 3. How to custom sort in columns always displaying null values at the end
              Hong Ju Newbie

              Hi Siva,

               

              Did you end up solving your issue? I use <rich:column comparator, but it does not sort when I click the column.

               

              Thanks,

               

              Lily

              • 4. Re: Richfaces 3. How to custom sort in columns always displaying null values at the end
                Bratislav Vukojicic Newbie

                Hi all,

                 

                I got similar issue where I needed to implement custom coparator class and did everything like Siva, except part:

                 

                    

                   public Comparator<Cmpt> getStartDateComparator(){

                      return new Comparator<Cmpt>() {

                             public int compare(Cmpt c1, Cmpt c2) {             

                             return c1.getStartDate().compareTo(c2.getStartDate());

                             }

                      };

                     }

                 

                this inner class was problematic, so I created "normal" comparator class like this:

                 

                public class NameColumnComparator implements Comparator<CustomerListItem>{

                 

                    public int compare(CustomerListItem c1, CustomerListItem c2){

                          return c1.getCustomerCompleteName().compareToIgnoreCase(c2.getCustomerCompleteName());

                    }

                }

                 

                and call my new comparator from frontend exposed class like this:

                 

                public someClass implements Serialazible {

                 

                private static final long serialVersionUID = -1L;

                private NameColumnComparator nameComparator = new NameColumnComparator();

                 

                public Comparator<CustomerListItem> getNameComparator(){

                        return nameComparator;

                    }

                }

                 

                I hope this solves a problem...