Locale Based Array sorting

php.internals

Derick Rethans

21 years ago
Hello, the attached patch adds a flag (SORT_LOCALE_STRING) to our sort functions that allows the sorting to be done based on the locale settings. This is important for languages like Norwegian which adds æ, ø and å to the end of the alphabet, while their iso-8859-1 (the "C" locale) order is å æ ø. The working is demonstrated as follows: <?php $a = array("ætte", "åtte", "øtte"); $d = $c = $b = $a; setlocale(LC_ALL, 'C'); sort($a); sort($b, SORT_LOCALE_STRING); var_dump($b); setlocale(LC_ALL, 'no_NO'); sort($c, SORT_LOCALE_STRING); var_dump($c); // Words in UTF-8 $d = array("Ã¥tte", "Þtte2", "Êtte4", "Þtte1", "Êtte3"); setlocale(LC_ALL, 'no_NO.UTF-8'); sort($d, SORT_LOCALE_STRING); var_dump($d); ?> Output: array(3) { [0]=> string(4) "åtte" [1]=> string(4) "ætte" [2]=> string(4) "øtte" } array(3) { [0]=> string(4) "ætte" [1]=> string(4) "øtte" [2]=> string(4) "åtte" } array(5) { [0]=> string(6) "Êtte3" [1]=> string(6) "Êtte4" [2]=> string(6) "Þtte1" [3]=> string(6) "Þtte2" [4]=> string(5) "Ã¥tte" } (The funny chars are UTf8, but it is correct sorting) I will be committing this patch later, if there are no objections. Derick
-- Derick Rethans http://derickrethans.nl | http://ez.no | http://xdebug.org