]> source.dussan.org Git - vaadin-framework.git/commitdiff
Fix for #2917 - BeanItemContainer.sort fails if property value is null
authorArtur Signell <artur.signell@itmill.com>
Fri, 8 May 2009 14:36:01 +0000 (14:36 +0000)
committerArtur Signell <artur.signell@itmill.com>
Fri, 8 May 2009 14:36:01 +0000 (14:36 +0000)
svn changeset:7691/svn branch:6.0

src/com/itmill/toolkit/data/util/BeanItemContainer.java

index 99e42e7e768524dfedb98041382a26c09f48ab6e..a2df599df2f1416d2688e777edb0b34d7d97e13a 100644 (file)
@@ -394,12 +394,31 @@ public class BeanItemContainer<BT> implements Indexed, Sortable, Filterable,
             Collections.sort(allItems, new Comparator<BT>() {
                 @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);
                 }
             });
         }