]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Fixed issue in method naturalCompare: strings having numbers with the same amount...
authorPeter Penz <peter.penz19@gmail.com>
Fri, 12 Jan 2007 21:21:06 +0000 (21:21 +0000)
committerPeter Penz <peter.penz19@gmail.com>
Fri, 12 Jan 2007 21:21:06 +0000 (21:21 +0000)
svn path=/trunk/playground/utils/dolphin/; revision=622757

src/dolphinsortfilterproxymodel.cpp
src/dolphinsortfilterproxymodel.h

index 34f7edc21e42f0e1f950fb0229a7148dcbe3db96..ad7c98b4de94c282b48a94ad634482eba6ab6544 100644 (file)
@@ -111,7 +111,7 @@ bool DolphinSortFilterProxyModel::lessThan(const QModelIndex& left,
 }
 
 int DolphinSortFilterProxyModel::naturalCompare(const QString& a,
-                                                const QString& b) const
+                                                const QString& b)
 {
     // This method chops the input a and b into pieces of
     // digits and non-digits (a1.05 becomes a | 1 | . | 05)
@@ -143,9 +143,9 @@ int DolphinSortFilterProxyModel::naturalCompare(const QString& a,
         }
 
         // compare these sequences
-        const QString sub_a(begSeqA, currA - begSeqA);
-        const QString sub_b(begSeqB, currB - begSeqB);
-        int cmp = QString::localeAwareCompare(sub_a, sub_b);
+        const QString subA(begSeqA, currA - begSeqA);
+        const QString subB(begSeqB, currB - begSeqB);
+        const int cmp = QString::localeAwareCompare(subA, subB);
         if (cmp != 0) {
             return cmp;
         }
@@ -157,7 +157,7 @@ int DolphinSortFilterProxyModel::naturalCompare(const QString& a,
         // now some digits follow...
         if ((*currA == '0') || (*currB == '0')) {
             // one digit-sequence starts with 0 -> assume we are in a fraction part
-            // do left aligned comparison (numbers are considered left aligend)
+            // do left aligned comparison (numbers are considered left aligned)
             while (1) {
                 if (!currA->isDigit() && !currB->isDigit()) {
                     break;
@@ -179,19 +179,21 @@ int DolphinSortFilterProxyModel::naturalCompare(const QString& a,
             }
         }
         else {
-            // no digit-sequence starts with 0 -> assume we are looking at some integer
-            // do right aligned comparison
-
-            // The longest run of digits wins.  That aside, the greatest
+            // No digit-sequence starts with 0 -> assume we are looking at some integer
+            // do right aligned comparison.
+            //
+            // The longest run of digits wins. That aside, the greatest
             // value wins, but we can't know that it will until we've scanned
             // both numbers to know that they have the same magnitude, so we
-            // remember it in 'weight'.
+            // remember the values in 'valueA' and 'valueB'.
+
+            int valueA = 0;
+            int valueB = 0;
 
-            int weight = 0;
             while (1) {
                 if (!currA->isDigit() && !currB->isDigit()) {
-                    if (weight != 0) {
-                        return weight;
+                    if (valueA != valueB) {
+                        return valueA - valueB;
                     }
                     break;
                 }
@@ -201,16 +203,14 @@ int DolphinSortFilterProxyModel::naturalCompare(const QString& a,
                 else if (!currB->isDigit()) {
                     return +1;
                 }
-                else if ((*currA < *currB) && (weight != 0)) {
-                    weight = -1;
-                }
-                else if ((*currA > *currB) && (weight != 0)) {
-                    weight = +1;
+                else {
+                    valueA = (valueA * 10) + currA->digitValue();
+                    valueB = (valueB * 10) + currB->digitValue();
                 }
+
                 ++currA;
                 ++currB;
             }
-
         }
 
         begSeqA = currA;
index 23cd451347802cea2052c7d02ddc76785d08264b..c8de791c6adbb61c1b97982979472db38d128d06 100644 (file)
@@ -66,7 +66,7 @@ protected:
                           const QModelIndex& right) const;
 
 private:
-    int naturalCompare(const QString& a, const QString& b) const;
+    static int naturalCompare(const QString& a, const QString& b);
 
 private:
     DolphinView::Sorting m_sorting;