Skip to Content
author's profile photo
Former Member

Table Column Sorting

Hi,

New year to all.

I would just like to give a gentle reminder on the 'Table Column Sorting' i had posted.

It would be great if you can provide me some sample code highlighting the use of

.sortElementXXX(Comparator)

.V

Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

1 Answer

  • author's profile photo
    Former Member
    Jan 09, 2004 at 02:38 PM

    Hi Vishal,

    And here the answer as well within this posting! Happy New Year to you as well!

    Best Regards,

    Karin

    The sort method asks everytime the comparator, if the method has to compare two lines, which one is "smaller" than the other one. In other words: within the code of the comparator is defined, which column has to be compared.

    An example: Assuming, we have a node "Customers", which I want to sort by attribute "name". The comparator would look like this:

    // create an anonoymous implementation of the interface Comparator

    Comparator comparator = new Comparator() {

    // this method compares two instances of the list (in our case

    // CustomersElements)

    int compare(Object a, Object b) {

    // the method gets Objects, but we know that they're

    // CustomersElements

    CustomersElement ea = (CustomersElement)a;

    CustomersElement eb = (CustomersElement)b;

    // compare the names (String.compareTo does exactly what we need)

    return ea.getName().compareTo(eb.getName());

    }

    }

    // sort using this comparator

    customersNode.sort(comparator);

    Add comment
    10|10000 characters needed characters exceeded