]> git.saurik.com Git - wxWidgets.git/commitdiff
Better sorting example and better default
authorRobert Roebling <robert@roebling.de>
Thu, 19 Jul 2007 14:03:43 +0000 (14:03 +0000)
committerRobert Roebling <robert@roebling.de>
Thu, 19 Jul 2007 14:03:43 +0000 (14:03 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@47562 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

samples/dataview/dataview.cpp
src/common/datavcmn.cpp

index 54cd4275176ba51a6b620757af6393cfcce7bc4d..95f61778144ceff35db975db85de10a154c88c4d 100644 (file)
@@ -167,6 +167,31 @@ public:
         ItemDeleted( item );
     }
     
+    // override sorting to always sort branches ascendingly
+    
+    int Compare( const wxDataViewItem &item1, const wxDataViewItem &item2 )
+    {
+        if (HasChildren(item1) && HasChildren(item2))
+        {
+            wxVariant value1,value2;
+            GetValue( value1, item1, 0 );
+            GetValue( value2, item2, 0 );
+
+            wxString str1 = value1.GetString();
+            wxString str2 = value2.GetString();
+            int res = str1.Cmp( str2 );
+            if (res) return res;
+            
+            // items must be different
+            unsigned long litem1 = (unsigned long) item1.GetID();    
+            unsigned long litem2 = (unsigned long) item2.GetID();    
+
+            return litem1-litem2;
+        }
+        
+        return wxDataViewModel::Compare( item1, item2 );
+    }
+
     // implementation of base class virtuals to define model
     
     virtual unsigned int GetColumnCount() const
index 4ae7be563f220f6c1fd82c8bb441a8db90a00500..acbc8da36ad1be00495d8f26c38e51a31b9878f0 100644 (file)
@@ -147,10 +147,19 @@ void wxDataViewModel::RemoveNotifier( wxDataViewModelNotifier *notifier )
 
 int wxDataViewModel::Compare( const wxDataViewItem &item1, const wxDataViewItem &item2 )
 {
+    // sort branches before leaves
+    bool item1_has_children = HasChildren(item1);
+    bool item2_has_children = HasChildren(item2);
+    
+    if (item1_has_children && !item2_has_children)
+        return 1;
+    if (item2_has_children && !item1_has_children)
+        return -1;
+    
     wxVariant value1,value2;
     GetValue( value1, item1, m_sortingColumn );
     GetValue( value2, item2, m_sortingColumn );
-    
+
     if (!m_ascending)
     {
         wxVariant temp = value1;
@@ -163,52 +172,37 @@ int wxDataViewModel::Compare( const wxDataViewItem &item1, const wxDataViewItem
         wxString str1 = value1.GetString();
         wxString str2 = value2.GetString();
         int res = str1.Cmp( str2 );
-        if (res == 0)
-        {
-            // no difference, try 0th column
-            if (m_sortingColumn != 0)
-            {
-                unsigned int temp = m_sortingColumn;
-                m_sortingColumn = 0;
-                res = Compare( item1, item2 );
-                m_sortingColumn = temp;
-            }
-            if (res == 0)
-            {
-                // still no difference, resort to desparate non-sense
-                long l1 = (long) item1.GetID();
-                long l2 = (long) item2.GetID();
-                return l1-l2;
-            }
-        }
-        return res;
-    }
+        if (res) return res;
+    } else
     if (value1.GetType() == wxT("long"))
     {
         long l1 = value1.GetLong();
         long l2 = value2.GetLong();
-        return l1-l2;
-    }
+        long res = l1-l2;
+        if (res) return res;
+    } else
     if (value1.GetType() == wxT("double"))
     {
         double d1 = value1.GetDouble();
         double d2 = value2.GetDouble();
-        if (d1 == d2) return 0;
         if (d1 < d2) return 1;
-        return -1;
-    }
+        if (d1 > d2) return -1;
+    } else
     if (value1.GetType() == wxT("datetime"))
     {
         wxDateTime dt1 = value1.GetDateTime();
         wxDateTime dt2 = value2.GetDateTime();
-        if (dt1.IsEqualTo(dt2)) return 0;
         if (dt1.IsEarlierThan(dt2)) return 1;
-        return -1;
+        if (dt2.IsEarlierThan(dt1)) return -11;
     }
-    
-    
 
-    return 0;
+    // items must be different
+    unsigned long litem1 = (unsigned long) item1.GetID();    
+    unsigned long litem2 = (unsigned long) item2.GetID();    
+
+    if (!m_ascending)
+        return litem2-litem1;
+    return litem1-litem2;
 }
 
 // ---------------------------------------------------------