need help with sorting words by utf-8. For example, we have 5 cities from Belgium.
$array = array('Borgloon','Thuin','Lennik','Éghezée','Aubel'); sort($array); // Expected: Aubel, Borgloon, Éghezée, Lennik, Thuin // Actual: Aubel, Borgloon, Lennik, Thuin, Éghezée
City Éghezée should be third. Is it possible to use/set some kind of utf-8 or create my own character order?
To sort a String array in Java, you need to compare each element of the array to all the remaining elements, if the result is greater than 0, swap them.
Approach: The usort() function is an inbuilt function in PHP which is used to sort the array of elements conditionally with a given comparator function. The usort() function can also be used to sort an array of objects by object field.
The ksort() function sorts an associative array in ascending order, according to the key. Tip: Use the krsort() function to sort an associative array in descending order, according to the key. Tip: Use the asort() function to sort an associative array in ascending order, according to the value.
intl comes bundled with PHP from PHP 5.3 and it only supports UTF-8.
You can use a Collator in this case:
$array = array('Borgloon','Thuin','Lennik','Éghezée','Aubel'); $collator = new Collator('en_US'); $collator->sort($array); print_r($array);
Output:
Array ( [0] => Aubel [1] => Borgloon [2] => Éghezée [3] => Lennik [4] => Thuin )
I think you can use strcoll:
setlocale(LC_COLLATE, 'nl_BE.utf8'); $array = array('Borgloon','Thuin','Lennik','Éghezée','Aubel'); usort($array, 'strcoll'); print_r($array);
Result:
Array ( [0] => Aubel [1] => Borgloon [2] => Éghezée [3] => Lennik [4] => Thuin )
You need the nl_BE.utf8 locale on your system:
fy@Heisenberg:~$ locale -a | grep nl_BE.utf8 nl_BE.utf8
If you are using debian you can use dpkg --reconfigure locales to add locales.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With