1

I have a page with a SELECT tag proposing to choose between multiple countries. The SELECT options are dynamically generated, a SQL query select all the countries from the database, not sorted, and add them into an array. Then, I use asort to sort the array and propose the SELECT. It works as long as there isn't accented characters, and accented character options are sorted after everything.

Check it on the page : http://worldisbeautiful.net/partager/?force_language=fr

  • make sure force_language=fr is on, otherwise it will be english by default
  • check the source code, you'll see 3 commented tests I made with the results

I tried, with no luck, solutions proposed on these questions :

For information, i receive sorted country data from databases but in english, then i localize them in french and due to complete name change it's unsorted (ie in french, united stats is états-unis).

Community
  • 1
  • 1
Aurélien Grimpard
  • 918
  • 10
  • 24
  • 1
    possible duplicate of [How to sort an array considering localization?](http://stackoverflow.com/questions/10273165/how-to-sort-an-array-considering-localization) – Arjan Oct 04 '13 at 23:15
  • I assume you can not do the sorting in the database? (that would be the easiest way, as you have more control over how to sort there) – Pekka Oct 04 '13 at 23:16
  • I actually do, but countries are stored in english and when they are localized, the order change (ie in french, "united states" is "états-unis"). – Aurélien Grimpard Oct 04 '13 at 23:18
  • 1
    Right. The duplicate above should help? – Pekka Oct 04 '13 at 23:26
  • Nope, tried it, no luck. – Aurélien Grimpard Oct 04 '13 at 23:30
  • There is also something weird, both `setlocale` and `collator` possible solutions show a completly unsorted array whereas `asort` all alone still sort unaccented countries. I don't understand what's going on :/ – Aurélien Grimpard Oct 05 '13 at 00:02
  • Are you working with UTF-8 data? – Pekka Oct 05 '13 at 00:41
  • Yes I do use UTF8. I'm considering to stop searching and go with a workaround like country localization in my database, this will resolve sorting issue. – Aurélien Grimpard Oct 05 '13 at 09:39
  • Possible duplicate of [Sort an array with special characters in PHP](http://stackoverflow.com/questions/10649473/sort-an-array-with-special-characters-in-php) – miken32 Oct 25 '16 at 17:55
  • Please post actual PHP code of what you tried and that indeed wouldn't sort right - "no luck" is too vague to help any further, while pinpointing could turn out your system just hasn't any French localization (for collations and such). – AmigoJack Aug 08 '19 at 13:23
  • You are 6 years late AmigoJack ;) – Aurélien Grimpard Sep 05 '19 at 12:02

0 Answers0