[TASK] Cleanup ArrayUtility::sortArraysByKey() 59/27559/4
authorMichiel Roos <michiel@maxserv.nl>
Tue, 11 Feb 2014 21:07:13 +0000 (22:07 +0100)
committerWouter Wolters <typo3@wouterwolters.nl>
Tue, 11 Feb 2014 22:02:42 +0000 (23:02 +0100)
Replace strtolower and ternary operations with strcasecmp.

Change-Id: Ia962ef879c6ba1d91081db78f74fc577fa67c467
Resolves: #55899
Releases: 6.2
Reviewed-on: https://review.typo3.org/27559
Reviewed-by: Oliver Klee
Reviewed-by: Christian Kuhn
Tested-by: Christian Kuhn
Reviewed-by: Wouter Wolters
Tested-by: Wouter Wolters
typo3/sysext/core/Classes/Utility/ArrayUtility.php

index 0793768..9edc1f6 100644 (file)
@@ -276,34 +276,23 @@ class ArrayUtility {
         *
         * @param array $arrays Array of arrays to sort
         * @param string $key Key to sort after
-        * @param integer $ascending Set to TRUE for ascending order, FALSE for descending order
+        * @param bool $ascending Set to TRUE for ascending order, FALSE for descending order
         * @return array Array of sorted arrays
         * @throws \RuntimeException
         */
        static public function sortArraysByKey(array $arrays, $key, $ascending = TRUE) {
-               if (!count($arrays))
+               if (empty($arrays)) {
                        return $arrays;
-
-               $sortResult = uasort($arrays, function($a, $b) use ($key, $ascending) {
+               }
+               $sortResult = uasort($arrays, function (array $a, array $b) use ($key, $ascending) {
                        if (!isset($a[$key]) || !isset($b[$key])) {
                                throw new \RuntimeException('The specified sorting key "' . $key . '" is not available in the given array.', 1373727309);
                        }
-                       $valueA = strtolower($a[$key]);
-                       $valueB = strtolower($b[$key]);
-                       if ($valueA === $valueB) {
-                               return 0;
-                       } else {
-                               if ($ascending) {
-                                       return $valueA > $valueB ? 1 : -1;
-                               } else {
-                                       return $valueA < $valueB ? 1 : -1;
-                               }
-                       }
+                       return ($ascending) ? strcasecmp($a[$key], $b[$key]) : strcasecmp($b[$key], $a[$key]);
                });
                if (!$sortResult) {
                        throw new \RuntimeException('The function uasort() failed for unknown reasons.', 1373727329);
                }
-
                return $arrays;
        }