New folder sort algorithm

Jan Schneider jan@horde.org
Fri, 21 Dec 2001 01:07:23 +0100


The folder sorting didn't work for me anymore, that is the folders weren't 
sorted correctly in the folder list, after the quicksort algorithm in the 
Zend engine was changed with the 4.1.0 release.
In short words: arrays with all elements equal aren't left untouched as in 
4.0.6 but get mixed which broke the former offset sorting in addLevel() and 
hsort().

Anyway, I fixed the sorting algorithm making it much simpler at the same 
time but could only test it on a Cyrus server, the only imap server at 
hand. Please try the patch with other servers before I commit it.

Jan.

Index: Tree.php
===================================================================
RCS file: /repository/imp/lib/Tree.php,v
retrieving revision 1.46
diff -u -r1.46 Tree.php
--- Tree.php	24 Oct 2001 19:17:03 -0000	1.46
+++ Tree.php	20 Dec 2001 22:50:21 -0000
@@ -295,10 +295,8 @@
     
     /* Hierarchy sorting functions */
     
-    function hsort($list, $off = 1)
+    function hsort($list)
     {
-        $this->offset = $off;
-        reset($list);
         usort($list, array($this, 'tree_cmp'));
         return $list;
     }
@@ -315,21 +313,16 @@
             return 1;
         }
         
-        if (strstr($a->name, $this->delimiter)) {
-            $tmp = explode($this->delimiter, $aname);
-            $aa = $tmp[count($tmp) - $this->offset];
-        } else {
-            $aa = $aname;
-        }
+        $aa = explode($this->delimiter, $aname);
+        $bb = explode($this->delimiter, $bname);
         
-        if (strstr($b->name, $this->delimiter)) {
-            $tmp = explode($this->delimiter, $bname);
-            $bb = $tmp[count($tmp) - $this->offset];
-        } else {
-            $bb = $bname;
+        for ($i = 0; $i < min(count($aa), count($bb)); $i++) {
+            if ($aa[$i] != $bb[$i]) {
+                return strcmp(strtolower($aa[$i]), strtolower($bb[$i]));
+            }
         }
         
-        return strcmp(strToLower($aa), strToLower($bb));
+        return count($aa) - count($bb);
     }
     
     
@@ -636,8 +629,7 @@
         }
         
         // sort the list
-        $list = $this->hsort($list, 1);
-        $list = $this->hsort($list, 2);
+        $list = $this->hsort($list);
         
         // toss them all in. stir well.
         $lastpid = '';


::::::::::::::::::::::::::::::::::::::::
AMMMa AG - discover your knowledge
:::::::::::::::::::::::::::
Detmolder Str. 25-33 :: D-33604 Bielefeld
fon +49.521.96878-0 :: fax  +49.521.96878-20
http://www.ammma.de
::::::::::::::::::::::::::::::::::::::::::::::