The problem is that Tapestry uses the usual Java String comparison for
sorting in Grid (generally speaking - Grid is not really localized).
String's compare uses the value of the character (and in UTF-8 encodings "b"
is greater than "G", as in ASCII).

So, if you want correct sorting you have to use a Collator.

Here's a correct GridDataSource (using a Collator on Strings):

/**
 * {@link GridDataSource} for {@link Collection}s with correct {@link
String} sorting.
 */
@SuppressWarnings("unchecked")
public class MyCollectionGridDataSource implements GridDataSource
{
        private final List list;

        private final Locale locale;

        private final Class<?> clazz;

        /**
         * Constructs a {@link GridDataSource} for the given collection (clazz 
is
determined by first
         * element in collection).
         * 
         * @param collection the collection
         * @param locale the current locale
         */
        public MyCollectionGridDataSource(final Collection collection, final 
Locale
locale)
        {
                this(collection, locale, null);
        }

        /**
         * Constructs a {@link GridDataSource} for the given collection.
         * 
         * @param collection the collection
         * @param locale the current locale
         * @param clazz which is the common super type of the objects in the
collection
         */
        public MyCollectionGridDataSource(final Collection collection, final 
Locale
locale, final Class<?> clazz)
        {
                Validate.notNull(collection, "collection");

                if (collection instanceof List)
                {
                        // Keep original list so that sorting it will reflect 
changes in UI
                        list = (List) collection;
                }
                else
                {
                        // Copy the collection so that we can sort it in a list
                        list = CollectionFactory.newList(collection);
                }

                this.locale = locale;

                if (clazz != null)
                {
                        this.clazz = clazz;
                }
                else
                {
                        this.clazz = list.isEmpty() ? null : 
list.get(0).getClass();
                }
        }

        public int getAvailableRows()
        {
                return list.size();
        }

        public void prepare(int startIndex, int endIndex, List<SortConstraint>
sortConstraints)
        {
                for (final SortConstraint constraint : sortConstraints)
                {
                        final ColumnSort sort = constraint.getColumnSort();

                        if (sort == ColumnSort.UNSORTED)
                                continue;

                        final PropertyConduit conduit =
constraint.getPropertyModel().getConduit();

            final Comparator valueComparator = new Comparator<Comparable>()
                        {
                                public int compare(Comparable o1, Comparable o2)
                                {
                                        // Simplify comparison, and handle case 
where both are nulls.

                                        if (o1 == o2)
                                                return 0;

                                        if (o2 == null)
                                                return 1;

                                        if (o1 == null)
                                                return -1;

                                        return (conduit.getPropertyType() == 
String.class) ?
Collator.getInstance(
                                                locale).compare(o1, o2) : 
o1.compareTo(o2);
                                }
                        };

                        final Comparator rowComparator = new Comparator()
                        {
                                public int compare(Object row1, Object row2)
                                {
                                        int modifier = sort == 
ColumnSort.ASCENDING ? 1 : -1;
                                        Comparable value1 = (Comparable) 
conduit.get(row1);
                                        Comparable value2 = (Comparable) 
conduit.get(row2);

                                        return modifier * 
valueComparator.compare(value1, value2);
                                }
                        };

                        // We can freely sort this list because its just a copy.
                        Collections.sort(list, rowComparator);
                }
        }

        /**
         * Returns the type of the first element in the list, or null if the 
list
is empty.
         */
        public Class getRowType()
        {
                return clazz;
        }

        public Object getRowValue(int index)
        {
                return list.get(index);
        }
}

You have to set the GridDataSource for your Grid or you add it to the
TypeCoercer.

-- 
Chris



darkslide wrote:
> 
> Hi,
> could you please help me with sorting of lowercase and uppercase for Grid?
> 
> Please see example:
> 
> I have output grid as below:
> 
> Name Description
> 
> A       ALPHA
> B       beta
> C       GAMA
> 
> When I click on Description label and sort it descending then row number
> two will be as first :
> 
> Name Description
> 
> B       beta
> C       GAMA
> A       ALPHA
> 
> Could you please tell me why? And tell me how can I repair the bug?
> 
> Thanks a lot for help
> 
> BR
> 
> Lukas Pavelka
> 


--
View this message in context: 
http://tapestry.1045711.n5.nabble.com/Grid-sorting-lowercase-uppercase-tp4887181p4892498.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
For additional commands, e-mail: users-h...@tapestry.apache.org

Reply via email to