thv20
thv20

Reputation: 978

How to sort an array considering localization?

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

Upvotes: 2

Views: 1758

Answers (2)

Mark Reed
Mark Reed

Reputation: 95252

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

Upvotes: 5

user267885
user267885

Reputation:

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?

Upvotes: 0

Related Questions