Feature #26485
closed
Natural sort with t3lib_div::naturalKeySortRecursive()
Added by Fedir RYKHTIK over 13 years ago.
Updated almost 7 years ago.
Description
This function uses strcasecmp() function to sort the array. But the real natural sorting should be implemened with strnatcasecmp().
So, the proposition is to change strcasecmp to strnatcasecmp in uksort second parameter. It will help to order complex array with numeric indexes.
Original function code :
/**
* Sorts an array by key recursive - uses natural sort order (aAbB-zZ)
*
* @param array $array array to be sorted recursively, passed by reference
* @return boolean TRUE if param is an array
*/
public static function naturalKeySortRecursive(&$array) {
if (!is_array($array)) {
return FALSE;
}
uksort($array, 'strcasecmp');
foreach ($array as $key => $value) {
self::naturalKeySortRecursive($array[$key]);
}
return TRUE;
}
Thanks You for the attention.
Do you use this method in your own code?
The core uses this method only in sysext lowlevel. It doesn't make any difference there.
- Status changed from New to Under Review
- Status changed from Under Review to New
Patch set 1 of change Ia9cd9a36da2bc0b6111a025e73b5bf8d26319c08 has been pushed to the review server.
It is available at http://review.typo3.org/1787
Patch set 2 of change Ia9cd9a36da2bc0b6111a025e73b5bf8d26319c08 has been pushed to the review server.
It is available at http://review.typo3.org/1787
This method is quite handy, when You need to sort complex array of TS. Instead of reinventing own sorting API, it's much better to use native core methods.
Example of TS array :
...
10 = a
1000.2 = bbbb
100 = aa
20 = bb
1000 = aaa
1000.10 = aaaa
2 = b
...
To sort such array You need to do exactly what this method should do : recursive natural sort by array keys.
Patch set 1 of change Ic378215707dfa0b20a198f82230afd56f1681934 has been pushed to the review server.
It is available at http://review.typo3.org/2109
Patch set 3 of change Ia9cd9a36da2bc0b6111a025e73b5bf8d26319c08 has been pushed to the review server.
It is available at http://review.typo3.org/1787
Patch set 4 of change Ia9cd9a36da2bc0b6111a025e73b5bf8d26319c08 has been pushed to the review server.
It is available at http://review.typo3.org/1787
Patch set 2 of change Ic378215707dfa0b20a198f82230afd56f1681934 has been pushed to the review server.
It is available at http://review.typo3.org/2109
Patch set 3 of change Ic378215707dfa0b20a198f82230afd56f1681934 has been pushed to the review server.
It is available at http://review.typo3.org/2109
- Status changed from New to Resolved
- % Done changed from 0 to 100
- Target version set to 4.5.4
- Status changed from Resolved to Closed
Also available in: Atom
PDF