问题
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:
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);
回答2:
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?
来源:https://stackoverflow.com/questions/10273165/how-to-sort-an-array-considering-localization