RE: How to make table sort case insensitive

The sorting is not performed by the Table but by the Container behind it.

For some of the standard in-memory containers (IndexedContainer and BeanItemContainer/BeanContainer), you can set a custom ItemSorter, which can be e.g. an instance of DefaultItemSorter with your customized property value comparator (given as a constructor parameter) that converts all strings to lower case.

If you are using other containers (such as lazily loading database containers), they need to support e.g. customized query construction, and you need to look at this on a container by container basis.

If you want to always sort strings case-insensitive (which might be a nice standardized default for a user interface), try this.


public class CaseInsensitiveItemSorter extends DefaultItemSorter {

	/**
	 * Constructs a CaseInsensitiveItemSorter that uses a case-insensitive sorter for string property values,
	 * and the default otherwise.
	 * 
	 */
	public CaseInsensitiveItemSorter() {
		super(new CaseInsensitivePropertyValueComparator());
	}

	/**
	 * Provides a case-insensitive comparator used for comparing string {@link Property} values.
	 * The <code>CaseInsensitivePropertyValueComparator</code> assumes all objects it
	 * compares can be cast to Comparable.
	 * 
	 */
	public static class CaseInsensitivePropertyValueComparator implements Comparator<Object>, Serializable {

		@SuppressWarnings("unchecked")
		public int compare(Object o1, Object o2) {
			int r = 0;
			// Normal non-null comparison
			if (o1 != null && o2 != null) {
				if ((o1 instanceof String) && (o2 instanceof String)) {
					return ((String) o1).compareToIgnoreCase((String) o2);
				} else {
					// Assume the objects can be cast to Comparable, throw
					// ClassCastException otherwise.
					r = ((Comparable<Object>) o1).compareTo(o2);
				}
			} else if (o1 == o2) {
				// Objects are equal if both are null
				r = 0;
			} else {
				if (o1 == null) {
					r = -1; // null is less than non-null
				} else {
					r = 1; // non-null is greater than null
				}
			}

			return r;
		}
	}

Tkz for this … helped me a lot!

I use it:

workerData = new BeanItemContainer(workers)
workerData.setItemSorter(new CaseInsensitiveItemSorter())
workerData.sort((Object[])['person']
, (boolean[])[true]
)

but it does not work, does not come in compare

How to disable sort for specific column in filtertable?

Overriding the getSortableContainerPropertyIds() method and only returning the propertyid’s that should be sorted should do it.

In my case used HierarchicalContainer()
And here is the solution:

HierarchicalContainer container = new HierarchicalContainer(); ... container.setItemSorter(new DefaultItemSorter(new SortIgnoreCase())); And SortIgnoreCase class:

public class SortIgnoreCase implements Comparator<Object> {
  public int compare(Object o1, Object o2) {
    String s1 = (String) o1;
    String s2 = (String) o2;
    return s1.toLowerCase().compareTo(s2.toLowerCase());
  }
}

let’s say I’d like to apply this sorting for strings but only for one specific column with strings (property ID of container). How can I figure out in inside compare method which property Id the string comes from? Or can I apply Item sorter not for the whole container but for only one propertyID?

I solved a similar problem by just overriding the compareProperty Method of the DefaultItemSorter and checked for my specific property and called super.compareProperty() when the propertyId was different from my specifc property.