# Locale Based Array sorting

- Thread: https://externals.io/message/12713
- Original archive: https://news-web.php.net/php.internals/12713
- Started by Derick Rethans on 2004-09-10T11:20:28+00:00
- Messages: 1
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #12713 — Derick Rethans — 2004-09-10T11:20:28+00:00

URL: https://externals.io/message/12713#12713

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
