7 Replies Latest reply on Nov 13, 2012 10:28 AM by Alejandro Profet

    Sorting table with custom function


      I am using JDev

      I have a table with records.

      public Record(int id, String desc) {
      this.id = id;
      this.description = desc;

      I want to implement a sorting function, for the description column, based on the length of the String.

      Any ideas how to do it?
        • 1. Re: Sorting table with custom function
          Sudipto Desmukh
          Some sample code here -
          1 person found this helpful
          • 2. Re: Sorting table with custom function
            Thanks for the link.

            I know that code and it doesn’t tell me how to define my own sort function.

            I was thinking about using a Comparator to define the sorting behavior (based on the string’s length) but, I don’t know how to apply it.
            • 3. Re: Sorting table with custom function
              Alejandro Profet
              Hi Pedro,

              You can add an attribute to the VO (if apply) with option "Mapped to Column or SQL" checked, and this sql expression:
              decode(YourViewObject.DESCRIPTION, null, 0, length(YourViewObject.DESCRIPTION))
              Hope this helps.

              • 4. Re: Sorting table with custom function
                Hi Alejandro,

                I'm not using VO's. My model is based on a java class.

                • 5. Re: Sorting table with custom function
                  Alejandro Profet
                  Hi Pedro,

                  In that case the class Record should be "comparable" as you said.
                  You can replace the current collection by an ordered list/set, and then implement the interface Comparator.
                  This is an example:
                  public java.util.TreeSet<Record> getRecords() {
                    TreeSet<Record> records = new TreeSet<Record>(new RecordComparator());
                    // routine to get the records
                    return records;
                  class RecordComparator implements Comparator<Record> {
                    public int compare(Record o1, Record o2) {
                      // be careful with null records/attributes!
                      return o1.getDescription().length() > o2.getDescription().length() ? 1 : o1.getDescription().length() < o2.getDescription().length() ? -1 : 0;
                  After that, try this:


                  Just add a transient attribute to class Record and try this with that new attribute: http://technology.amis.nl/2012/03/01/adf-11g-programmatically-configuring-sort-for-rich-table/


                  Edited by: Alejandro Profet on Nov 12, 2012 3:32 PM
                  • 6. Re: Sorting table with custom function
                    Hi Alejandro,

                    Replacing the current collection by a TreeSet<Record> and providing the comparator, the records are first shown in the table in accordance wit the sorting fuction (comparator).

                    However, when I click in the sorting arrows (description column), records are sorted based on the default sorting for strings (alphabetically). In order to avoid this behavior, I added one more attribute to the Record class which gives me the descripition's length. Therefore, everytime we click the arrows to sort, I changed the sort criteria as follows:

                    newSortCriteria.add(new SortCriterion("descriptionLength", sortCriterion.isAscending()));

                    This way I have the initial question answered and thanks for helping me.

                    Furthermore, I would like to know if it is possible to avoid the extra length attribute on Records? I mean, is it possible to link the sorting event, triggered by the column arrows, with the Comparator function?
                    • 7. Re: Sorting table with custom function
                      Alejandro Profet
                      Hi Pedro,

                      You can create the comparator as variable in the managed bean who implements the sortListener, in order to pass custom parameters to the comparator class (extra length, direction asc/desc, etc).
                      I don't know if this is what you're looking for.