[BUGFIX] Natural sort with t3lib_div::naturalKeySortRecursive()
authorMarkus Klein <klein.t3@mfc-linz.at>
Thu, 12 May 2011 20:10:19 +0000 (22:10 +0200)
committerAndreas Wolf <andreas.wolf@ikt-werk.de>
Thu, 2 Jun 2011 10:23:17 +0000 (12:23 +0200)
This fix changes the used compare function for
t3lib_div::naturalKeySortRecursive() from strcasecmp to strnatcasecmp.
This ensures >natural< sorting especially in conjunction with
numbers in strings.

Change-Id: Ic378215707dfa0b20a198f82230afd56f1681934
Resolves: #26485
Releases: 4.6, 4.5
Reviewed-on: http://review.typo3.org/2109
Reviewed-by: Andreas Wolf
Tested-by: Andreas Wolf
t3lib/class.t3lib_div.php
tests/t3lib/t3lib_divTest.php

index a55987a..26da869 100644 (file)
@@ -2279,7 +2279,7 @@ final class t3lib_div {
                if (!is_array($array)) {
                        return FALSE;
                }
-               uksort($array, 'strcasecmp');
+               uksort($array, 'strnatcasecmp');
                foreach ($array as $key => $value) {
                        self::naturalKeySortRecursive($array[$key]);
                }
index 34c4302..e81b5cc 100644 (file)
@@ -1281,6 +1281,119 @@ class t3lib_divTest extends tx_phpunit_testcase {
                $this->assertEquals($expectedResult, t3lib_div::removeDotsFromTS($typoScript));
        }
 
+       //////////////////////////////////////
+       // Tests concerning naturalKeySortRecursive
+       //////////////////////////////////////
+
+       /**
+        * @test
+        */
+       public function naturalKeySortRecursiveReturnsFalseIfInputIsNotAnArray() {
+               $testValues = array(
+                       1,
+                       'string',
+                       FALSE
+               );
+               foreach($testValues as $testValue) {
+                       $this->assertFalse(t3lib_div::naturalKeySortRecursive($testValue));
+               }
+       }
+
+       /**
+        * @test
+        */
+       public function naturalKeySortRecursiveSortsOneDimensionalArrayByNaturalOrder() {
+               $testArray = array(
+                       'bb' => 'bb',
+                       'ab' => 'ab',
+                       '123' => '123',
+                       'aaa' => 'aaa',
+                       'abc' => 'abc',
+                       '23' => '23',
+                       'ba' => 'ba',
+                       'bad' => 'bad',
+                       '2' => '2',
+                       'zap' => 'zap',
+                       '210' => '210'
+               );
+               $expectedResult = array(
+                       '2',
+                       '23',
+                       '123',
+                       '210',
+                       'aaa',
+                       'ab',
+                       'abc',
+                       'ba',
+                       'bad',
+                       'bb',
+                       'zap'
+               );
+               t3lib_div::naturalKeySortRecursive($testArray);
+               $this->assertEquals($expectedResult, array_values($testArray));
+       }
+
+       /**
+        * @test
+        */
+       public function naturalKeySortRecursiveSortsMultiDimensionalArrayByNaturalOrder() {
+               $testArray = array(
+                       '2' => '2',
+                       'bb' => 'bb',
+                       'ab' => 'ab',
+                       '23' => '23',
+                       'aaa' => array(
+                               'bb' => 'bb',
+                               'ab' => 'ab',
+                               '123' => '123',
+                               'aaa' => 'aaa',
+                               '2' => '2',
+                               'abc' => 'abc',
+                               'ba' => 'ba',
+                               '23' => '23',
+                               'bad' => array(
+                                       'bb' => 'bb',
+                                       'ab' => 'ab',
+                                       '123' => '123',
+                                       'aaa' => 'aaa',
+                                       'abc' => 'abc',
+                                       '23' => '23',
+                                       'ba' => 'ba',
+                                       'bad' => 'bad',
+                                       '2' => '2',
+                                       'zap' => 'zap',
+                                       '210' => '210'
+                               ),
+                               '210' => '210',
+                               'zap' => 'zap'
+                       ),
+                       'abc' => 'abc',
+                       'ba' => 'ba',
+                       '210' => '210',
+                       'bad' => 'bad',
+                       '123' => '123',
+                       'zap' => 'zap'
+               );
+
+               $expectedResult = array(
+                       '2',
+                       '23',
+                       '123',
+                       '210',
+                       'aaa',
+                       'ab',
+                       'abc',
+                       'ba',
+                       'bad',
+                       'bb',
+                       'zap'
+               );
+               t3lib_div::naturalKeySortRecursive($testArray);
+
+               $this->assertEquals($expectedResult, array_values(array_keys($testArray['aaa']['bad'])));
+               $this->assertEquals($expectedResult, array_values(array_keys($testArray['aaa'])));
+               $this->assertEquals($expectedResult, array_values(array_keys($testArray)));
+       }
 
        //////////////////////////////////////
        // Tests concerning get_dirs