2

I'm trying to sort an array of names alphabetically (Croatian in this case).

How can I get Đani to show up before Derrick?

$names = array(
    "Đani", "Bill", "Dennis", "George", "Derrick"
);

sort($names);

print_r($names);
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
thv20
  • 986
  • 4
  • 13
  • 26
  • possible duplicate of [Natural sorting algorithm in PHP with support for Unicode?](http://stackoverflow.com/questions/832709/natural-sorting-algorithm-in-php-with-support-for-unicode) – John Conde Apr 22 '12 at 23:49

2 Answers2

5

You need to set the locale appropriately, probably like this:

setlocale(LC_ALL, 'hr_HR');

And then tell sort to honor the locale:

sort($names,SORT_LOCALE_STRING);
Mark Reed
  • 91,912
  • 16
  • 138
  • 175
0

If you can, you can import them into a MySQL table and use the ORDER BY clause to sort, provided you set the right collation for the database/table.

I am sure that there are simpler solutions not requiring a RDMS though.

Take a look to that question as well: Natural sorting algorithm in PHP with support for Unicode?

Community
  • 1
  • 1
  • This was my first port of call, but my host is running mYSQL 5.1 which does not support Croatian collation. So SORT BY is effectively broken. I believe this was fixed in 5.6. – thv20 Apr 23 '12 at 17:21