From 004426c26d524c2931bbf1a4527c746d0a0948ac Mon Sep 17 00:00:00 2001 From: Torben Brodt Date: Mon, 30 Jan 2012 20:25:35 +0100 Subject: [PATCH] 90% performance optimization with spaces --- .../files/lib/data/DatabaseObject.class.php | 63 ++++++------------- 1 file changed, 19 insertions(+), 44 deletions(-) diff --git a/wcfsetup/install/files/lib/data/DatabaseObject.class.php b/wcfsetup/install/files/lib/data/DatabaseObject.class.php index d7ffd13c51..b2cddd2068 100644 --- a/wcfsetup/install/files/lib/data/DatabaseObject.class.php +++ b/wcfsetup/install/files/lib/data/DatabaseObject.class.php @@ -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 +} -- 2.20.1