90% performance optimization with spaces
authorTorben Brodt <t.brodt@gmail.com>
Mon, 30 Jan 2012 19:25:35 +0000 (20:25 +0100)
committerTorben Brodt <t.brodt@gmail.com>
Mon, 30 Jan 2012 19:25:35 +0000 (20:25 +0100)
wcfsetup/install/files/lib/data/DatabaseObject.class.php

index d7ffd13c517b2768ecab4202ac38a24ac16d8e72..b2cddd2068bea61ea0e10c0db06fc9fb15d27c1f 100644 (file)
@@ -135,7 +135,7 @@ abstract class DatabaseObject implements IStorableObject {
        public static function getDatabaseTableIndexName() {
                return static::$databaseTableIndexName;
        }
-       
+
        /**
         * Sorts a list of database objects.
         *
@@ -145,51 +145,26 @@ abstract class DatabaseObject implements IStorableObject {
         * @return      boolean
         */
        public static function sort(&$objects, $sortBy, $sortOrder = 'ASC', $maintainIndexAssociation = true) {
-               static::$sortBy = (!is_array($sortBy) ? array($sortBy) : $sortBy);
-               static::$sortOrder = (!is_array($sortOrder) ? array($sortOrder) : $sortOrder);
+               $sortArray = $objects2 = array();
+               foreach ($objects as $idx => $obj) {
+                       $sortArray[$idx] = $obj->$sortBy;
 
-               if ($maintainIndexAssociation) {
-                       return uasort($objects, array('static', 'compareObjects'));
-               }
-               else {
-                       return usort($objects, array('static', 'compareObjects'));
-               }
-       }
-       
-       /**
-        * Compares two database objects.
-        *
-        * @param       wcf\data\DatabaseObject         $objectA
-        * @param       wcf\data\DatabaseObject         $objectB
-        * @return      float
-        */
-       protected static function compareObjects($objectA, $objectB) {
-               foreach (static::$sortBy as $key => $sortBy) {
-                       $sortOrder = (isset(static::$sortOrder[$key]) ? static::$sortOrder[$key] : 'ASC');
-                       $valA = $objectA->$sortBy;
-                       $valB = $objectB->$sortBy;
-                       if (is_numeric($valA) && is_numeric($valB)) {
-                               if ($valA > $valB) {
-                                       return ($sortOrder == 'ASC' ? 1 : 0);
-                               }
-                               else if ($valA < $valB) {
-                                       return ($sortOrder == 'ASC' ? 0 : 1);
-                               }
+                       // array_multisort will drop index association if key is not a string
+                       if ($maintainIndexAssociation) {
+                               $objects2[$idx.'x'] = $obj;
                        }
-                       else {
-                               if ($sortOrder == 'ASC') {
-                                       $result =  strcoll($valA, $valB);
-                               }
-                               else {
-                                       $result = strcoll($valB, $valA);
-                               }
-                               
-                               if ($result != 0.0) {
-                                       return $result;
-                               }
+               }
+
+               if ($maintainIndexAssociation) {
+                       $objects = array();
+                       array_multisort($sortArray, $sortOrder == 'ASC' ? SORT_ASC : SORT_DESC, $objects2);
+
+                       $objects = array();
+                       foreach ($objects2 as $idx => $obj) {
+                               $objects[substr($idx, 0, -1)] = $obj;
                        }
+               } else {
+                       array_multisort($sortArray, $sortOrder == 'ASC' ? SORT_ASC : SORT_DESC, $objects);
                }
-               
-               return 0;
        }
-}
\ No newline at end of file
+}