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);
1

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Damnit, you posted the exact same answer as me, but I just took a while to find the locale code for Croatia. +1
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?

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.