5 Replies Latest reply on Feb 10, 2012 2:06 PM by Rajesh Gowda

    Custom sorting in <rich:scrollableDataTable>

    Rajesh Gowda Newbie

      Hello,

       

      i am a new bie in richfaces, i use richfaces 3.3.1

      i used rich:scrollableDataTable in my application and used the default sorting to sort columns which sorts column based on the datatype of the column

       

      Now i have a column whose datatype is String, i have the following data

       

      ABC

      ABC1

      ABC2

      ABC10

      ABC12

      ABC3

       

      After sorting i get

       

      ABC

      ABC1

      ABC10

      ABC12

      ABC2

      ABC3

       

      Which is not my intention, i would like to sort the column like the below,

       

      ABC

      ABC1

      ABC2

      ABC3

      ABC10

      ABC12

       

      How can i achieve this ?????

       

      Thanks in advance.

       

      rAj.

        • 1. Re: Custom sorting in <rich:scrollableDataTable>
          Ilya Shaikovsky Master

          define your comparator using column attribute.

          • 2. Re: Custom sorting in <rich:scrollableDataTable>
            Rajesh Gowda Newbie

            i used the comparator like below,

             

             

            <rich:scrollableDataTable id="subnw" height="400px" width="700px" rows="30"
            value="#{dataTableScrollerBean.allCars}" var="category">
            <rich:column sortExpression="#{category.makeComparator}" id="make">
            <f:facet name="header">Mak</f:facet>
            <h:outputText value="#{category.make}" />
            </rich:column>
            Now the question is where should i have the comparator ???
            i had kept it in the backingbean, i had a property which is a comparator. is my assumption correct ????
            still the sorting is not working.
            BR,
            rAj
            • 3. Re: Custom sorting in <rich:scrollableDataTable>
              Rajesh Gowda Newbie

              i got it working :-)

               

              i used it like the following,

               

               

              <rich:scrollableDataTable id="subnw" height="400px" width="700px" rows="30"
              value="#{dataTableScrollerBean.allCars}" var="category">
              <rich:column sortExpression="#{dataTableScrollerBean.makeComparator}" id="make">
              <f:facet name="header">Mak</f:facet>
              <h:outputText value="#{category.make}" />
              </rich:column>

               

              where i have written the comparator in my bbean.

               

              BR,

              Rajesh

              • 4. Re: Custom sorting in <rich:scrollableDataTable>
                leo lee Newbie

                Hi Rajesh,

                I have the same question but I'm not sure how to implement the comparator method.

                 

                Can you please post your method dataTableScrollerBean.makeComparator() here? Thanks.

                 

                Hongqian Liu

                • 5. Re: Custom sorting in <rich:scrollableDataTable>
                  Rajesh Gowda Newbie

                  Hello Liu,

                   

                  Following is a sample code...hope it helps you.

                   

                  public class MakeComparator implements Comparator<ManagedObject>,Serializable{

                   

                   

                      /**

                             *  Serializable

                             */

                            private static final long serialVersionUID = -8957009177209642023L;

                   

                   

                            //Argument can be of what ever type you would like to compare

                            public int compare(String o1, String o2) {

                          //Implement your custom comparator here

                          if(o1.compareTo(o2)>1){

                              return 1;

                          }else if(o1.compareTo(o2)<1){

                              return 1;

                          }else{

                              return 0;

                          }

                      }

                   

                   

                  }

                   

                   

                  And in the bean

                   

                  public MakeComparator getKeyComparator() {

                          return keyComparator;

                      }

                   

                   

                      public void setKeyComparator(MakeComparator keyComparator) {

                          this.keyComparator = keyComparator;

                      }

                   

                   

                   

                  BR,

                  Rajesh