2

i have some problems with sorting an array.

List

0 => string 'Australien' (length=10)
1 => string 'Belgien' (length=7)
2 => string 'Botswana' (length=8)
3 => string 'Brasilien' (length=9)
4 => string 'Bulgarien' (length=9)
5 => string 'Burma' (length=5)
6 => string 'China' (length=5)
7 => string 'Costa Rica' (length=10)
73 => string 'Ägypten' (length=8)

But Ägypten should be after Australien. I already tried with the Collator class but our client wont install the extension.

noaaah
  • 45
  • 1
  • 8

1 Answers1

2

You can use setlocale along with first parameter LC_COLLATE and second locale with en_US.utf8 and simply sort using usort along with strcoll try as

setlocale(LC_COLLATE, 'en_US.utf8');
$array = array('Australien','Belgien','Botswana','Brasilien','Bulgarien','Burma','China','Costa Rica','Ägypten');
usort($array, 'strcoll'); 
print_r($array);

Demo

Narendrasingh Sisodia
  • 20,667
  • 5
  • 43
  • 53
  • In this case the locale should probably be `de_DE.UTF-8`, but yes, this *should* work. – deceze Aug 20 '15 at 09:08
  • Thanks for reffering. I'll update it but it'll generate the same as that of shown by OP [Check This](https://eval.in/419493) @deceze – Narendrasingh Sisodia Aug 20 '15 at 09:09
  • Its wont work... ` setlocale(LC_COLLATE, 'de_DE.UTF-8'); $this->utf8_encode_deep($list); usort($list['continents'], 'strcoll'); die(var_dump($list['countries']));` – noaaah Aug 20 '15 at 09:21
  • What's not working @noaaah did you checked the [demo link its working fine](https://eval.in/419488) – Narendrasingh Sisodia Aug 20 '15 at 09:22
  • @NarendraSisodia Well idk for me it wont work i put my code in the comment – noaaah Aug 20 '15 at 09:27
  • Well I've replaced `de_DE.UTF-8` along with `en_US.utf8` to let it work as required @noaaah – Narendrasingh Sisodia Aug 20 '15 at 09:36
  • @noaaah 1) What in the world does `utf8_encode_deep` do?! 2) You need to ensure that those locales you're setting are actually installed on the system. Perhaps they're named differently on yours. – deceze Aug 20 '15 at 09:53