Hi You're going to need to implement your own Comparator, and set it on the list used by the TableView.
Or you could use something like the attached class, which allows you to set your own comparator for a column. -- Noel Grandin Giguère, Philippe wrote: > > Hi, > > > > I have a TableView which allow to sort data. Everything works well except > when the data are images. I can't sort the > images like I want and the sort result isn't the same at each execution of > the program. > > > > Example of results: > > First execution = Image1, Image1, Image2, Image2, Image3. > > Second execution = Image3, Image1, Image1, Image2, Image2. > > > > I searched why the result weren't never the same and I found that the > TableViewRowComparator use the > Comparable.compareTo(Object) method if the object implement Compare. If not, > it use the toString() method that use the > hash code of the object to sort. So, the hash code change for the object at > each execution and it change the sort > order as well at each execution. > > > > I was wondering if there are a way already implented in Pivot to sort images > or if you have some clue to do it? If not > that could be an nice thing to add in the next version of Pivot. > > > > Thx a lot > > > > Philippe Giguère > > ------------------------------------------------------------------------------------------------------------------------ > *Avis de confidentialité:* Ce courriel et les pièces qui y sont jointes > peuvent contenir des renseignements > confidentiels qui ne vous sont pas destinés. Si vous avez reçu cette > correspondance par erreur, veuillez la détruire > et nous en aviser. Merci! > > Devez-vous vraiment imprimer ce courriel ? Pensons à l'environnement ...
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.pivot.wtk.content; import java.util.Comparator; import org.apache.pivot.beans.BeanAdapter; import org.apache.pivot.collections.Dictionary; import org.apache.pivot.collections.HashMap; import org.apache.pivot.collections.Map; import org.apache.pivot.wtk.SortDirection; import org.apache.pivot.wtk.TableView; /** * Compares two rows in a table view. */ public class TableViewRowComparator2 implements Comparator<Object> { private TableView tableView; private Map<String, Comparator<Object>> comparatorOverrides; public TableViewRowComparator2(TableView tableView) { if (tableView == null) { throw new IllegalArgumentException(); } this.tableView = tableView; } /** * Compares two rows in a table view. If the column values implement * {...@link Comparable}, the {...@link Comparable#compareTo(Object)} method will be used * to compare the values. Otherwise, the values will be compared as strings using * {...@link Object#toString()}. If either value is <tt>null</tt>, it will be * considered as less than the other value. If both values are <tt>null</tt>, they * will be considered equal. */ @Override @SuppressWarnings("unchecked") public int compare(Object o1, Object o2) { int result; TableView.SortDictionary sort = tableView.getSort(); if (sort.getLength() > 0) { Dictionary<String, ?> row1; if (o1 instanceof Dictionary<?, ?>) { row1 = (Dictionary<String, ?>)o1; } else { row1 = new BeanAdapter(o1); } Dictionary<String, ?> row2; if (o2 instanceof Dictionary<?, ?>) { row2 = (Dictionary<String, ?>)o2; } else { row2 = new BeanAdapter(o2); } result = 0; int n = sort.getLength(); int i = 0; while (i < n && result == 0) { Dictionary.Pair<String, SortDirection> pair = sort.get(i); String columnName = pair.key; SortDirection sortDirection = sort.get(columnName); Object value1 = row1.get(columnName); Object value2 = row2.get(columnName); if (value1 == null && value2 == null) { result = 0; } else if (value1 == null) { result = -1; } else if (value2 == null) { result = 1; } else { if (comparatorOverrides != null && comparatorOverrides.containsKey(columnName)) { result = comparatorOverrides.get(columnName).compare(value1, value2); } else if (value1 instanceof Comparable<?>) { result = ((Comparable<Object>)value1).compareTo(value2); } else { String s1 = value1.toString(); String s2 = value2.toString(); result = s1.compareTo(s2); } } result *= (sortDirection == SortDirection.ASCENDING ? 1 : -1); i++; } } else { result = 0; } return result; } /** * Allows setting of an explicit comparator to use for a column */ public void setComparator(String columnName, Comparator<Object> comparator) { if (comparatorOverrides == null) { comparatorOverrides = new HashMap<String, Comparator<Object>>(); } comparatorOverrides.put(columnName, comparator); } }
