From: Artur Signell Date: Fri, 8 May 2009 14:36:01 +0000 (+0000) Subject: Fix for #2917 - BeanItemContainer.sort fails if property value is null X-Git-Tag: 6.7.0.beta1~2934 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=36ff35c304321ff4cfd4221cdc9ccf4c2d9f64ff;p=vaadin-framework.git Fix for #2917 - BeanItemContainer.sort fails if property value is null svn changeset:7691/svn branch:6.0 --- diff --git a/src/com/itmill/toolkit/data/util/BeanItemContainer.java b/src/com/itmill/toolkit/data/util/BeanItemContainer.java index 99e42e7e76..a2df599df2 100644 --- a/src/com/itmill/toolkit/data/util/BeanItemContainer.java +++ b/src/com/itmill/toolkit/data/util/BeanItemContainer.java @@ -394,12 +394,31 @@ public class BeanItemContainer implements Indexed, Sortable, Filterable, Collections.sort(allItems, new Comparator() { @SuppressWarnings("unchecked") public int compare(BT a, BT b) { - Comparable va = (Comparable) beanToItem.get(a) - .getItemProperty(property).getValue(); - Comparable vb = (Comparable) beanToItem.get(b) - .getItemProperty(property).getValue(); - - return asc ? va.compareTo(vb) : vb.compareTo(va); + Comparable va, vb; + if (asc) { + va = (Comparable) beanToItem.get(a).getItemProperty( + property).getValue(); + vb = (Comparable) beanToItem.get(b).getItemProperty( + property).getValue(); + } else { + va = (Comparable) beanToItem.get(b).getItemProperty( + property).getValue(); + vb = (Comparable) beanToItem.get(a).getItemProperty( + property).getValue(); + } + + /* + * Null values are considered less than all others. The + * compareTo method cannot handle null values for the + * standard types. + */ + if (va == null) { + return (vb == null) ? 0 : -1; + } else if (vb == null) { + return (va == null) ? 0 : 1; + } + + return va.compareTo(vb); } }); }