restricteur
restricteur

Reputation: 312

Displaytag global order is Ok but order on the same page is reversed

I am using dsiplaytag with external sorting and partial lists. When I sort the table using a certain column containing a numerical value (duration) the whole list globally is in order (descending for example) but on each page it is ascending. Here is an example :

Page 1:
31
32
33
Page 2:
28
29
30
Page 3:
25
26
27

While What I expect is :

Page 1:
33
32
31
Page 2:
30
29
28
Page 3:
27
26
25

Here is the declaration of my table :

<display:table name="operations" class="list" requestURI=""
            id="operation" pagesize="${applicationScope.pageSize}" partialList="true" size="listSize" sort="external" cellpadding="${paddingValue}"
            export="true">

The even wierder thing is that in my java code I tried to reverse the order of the list operations but it seems that displaytag is displaying the list in a reversed order no matter in what order I send the list.

Any ideas about this behaviour ?

Upvotes: 2

Views: 626

Answers (1)

restricteur
restricteur

Reputation: 312

After more than a week of struggling I found the solution: use a custom comparator instead of the displaytag default one (org.displaytag.DefaultComparator):

Here is the code of the comparator :

package com.example.model;    

import java.text.Collator;
import java.util.Comparator;

public class CustomComparator implements Comparator
{

    private Collator collator;

    public CustomComparator()
    {
        this(Collator.getInstance());
    }

    public CustomComparator(Collator collatorToUse)
    {
        this.collator = collatorToUse;
        this.collator.setStrength(0);
    }

    public int compare(Object object1, Object object2)
    {
        int returnValue;
        if ((object1 instanceof String) && (object2 instanceof String))
        {
            returnValue = this.collator.compare(object1, object2);
        }
        else
        {
            if ((object1 instanceof Comparable) && (object2 instanceof Comparable))
            {
                returnValue = ((Comparable) object1).compareTo(object2);
            }
            else
            {
                returnValue = this.collator.compare(object1.toString(), object2.toString());
            }
        }
        return -returnValue;
    }

}

The only difference with the default comparator is the last instruction in the compare method: -returnValue instead of returnValue.

Then, in your displaytag.properties tell displaytag that you would like to use your comparator:

comparator.default=com.example.model.CustomComparator

And that resolved my problem.

Upvotes: 2

Related Questions