This discussion is archived
7 Replies Latest reply: Nov 13, 2012 2:28 AM by AlejandroProfet RSS

Sorting table with custom function

755976 Newbie
Currently Being Moderated
Hi,

I am using JDev 11.1.1.4.0

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 Expert
    Currently Being Moderated
    Some sample code here -
    https://kr.forums.oracle.com/forums/thread.jspa?threadID=2215741
  • 2. Re: Sorting table with custom function
    755976 Newbie
    Currently Being Moderated
    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
    AlejandroProfet Journeyer
    Currently Being Moderated
    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.

    AP
  • 4. Re: Sorting table with custom function
    755976 Newbie
    Currently Being Moderated
    Hi Alejandro,

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

    Thanks.
  • 5. Re: Sorting table with custom function
    AlejandroProfet Journeyer
    Currently Being Moderated
    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> {
      @Override
      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:
    http://technology.amis.nl/2012/03/01/adf-11g-programmatically-configuring-sort-for-rich-table/

    AP

    Correction:
    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/

    AP

    Edited by: Alejandro Profet on Nov 12, 2012 3:32 PM
  • 6. Re: Sorting table with custom function
    755976 Newbie
    Currently Being Moderated
    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()));
    table.setSortCriteria(newSortCriteria);
    (...)

    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
    AlejandroProfet Journeyer
    Currently Being Moderated
    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.

    AP

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points