← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 6824: Fixed bug with ListGrid sorting

 

------------------------------------------------------------
revno: 6824
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2012-05-01 16:21:21 +0200
message:
  Fixed bug with ListGrid sorting
modified:
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/ListGrid.java
  dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/grid/GridTest.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/ListGrid.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/ListGrid.java	2012-04-26 07:56:54 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/ListGrid.java	2012-05-01 14:21:21 +0000
@@ -378,6 +378,11 @@
 
     public Grid sortGrid( int columnIndex, int order )
     {
+        if ( order == 0 )
+        {
+            return this; // No sorting
+        }
+        
         columnIndex = columnIndex - 1;
 
         if ( columnIndex < 0 || columnIndex >= getWidth() )
@@ -639,19 +644,20 @@
         @SuppressWarnings( "unchecked" )
         public int compare( List<Object> list1, List<Object> list2 )
         {
-            if ( order == 0 )
+            boolean list1Invalid = list1 == null || list1.get( columnIndex ) == null || !(list1.get( columnIndex ) instanceof Comparable<?>);
+            boolean list2Invalid = list2 == null || list2.get( columnIndex ) == null || !(list2.get( columnIndex ) instanceof Comparable<?>);
+            
+            if ( list1Invalid && list2Invalid )
             {
                 return 0;
             }
-
-            if ( list1 == null || list1.get( columnIndex ) == null || !(list1.get( columnIndex ) instanceof Comparable<?>) )
+            else if ( list1Invalid )
             {
-                return 1; // Null comes last
+                return order > 0 ? 1 : -1;
             }
-
-            if ( list2 == null || list2.get( columnIndex ) == null || !(list2.get( columnIndex ) instanceof Comparable<?>) )
+            else if ( list2Invalid )
             {
-                return -1; // Null comes last
+                return order > 0 ? -1 : 1; 
             }
 
             final Comparable<Object> value1 = (Comparable<Object>) list1.get( columnIndex );

=== modified file 'dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/grid/GridTest.java'
--- dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/grid/GridTest.java	2012-02-20 10:20:01 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/grid/GridTest.java	2012-05-01 14:21:21 +0000
@@ -393,15 +393,15 @@
         grid.addRow().addValue( "one" ).addValue( 1 );
         
         grid.sortGrid( 2, -1 );
-        
+
         List<Object> row1 = grid.getRow( 0 );
-        assertTrue( row1.contains( "one" ) );
-
+        assertTrue( row1.contains( "null" ) );
+        
         List<Object> row2 = grid.getRow( 1 );
-        assertTrue( row2.contains( "two" ) );
-        
+        assertTrue( row2.contains( "one" ) );
+
         List<Object> row3 = grid.getRow( 2 );
-        assertTrue( row3.contains( "null" ) );
+        assertTrue( row3.contains( "two" ) );        
     }
     
     @Test