How to sort an array considering localization?

社会主义新天地 提交于 2020-01-15 08:10:20

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!