Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignoring diacritics when ordering alphabetically

I'm making a Java app that receives some names from SQLite and puts them on a listbox. The thing is I want it to be accurately ordered in an ascending alphabetical way (In Portuguese to be specific).

These entries, for example:

Beta Árida Ana

Should be ordered as:

Ana Árida Beta

But since it orders in some ASCII order, the "accented" characters on will be thrown at the end and not right below the letter they correspond to.

Result: Ana Beta Árida

How can I solve this? EDIT: I meant resolving the issue with Java itself and not with SQlite improvements

Thanks in advance.

like image 526
Qosmo Avatar asked Sep 01 '10 02:09

Qosmo


2 Answers

You can read the names first into a regular List<String>, and then use Collections.sort() to sort the list. To specify a locale-sensitive ordering, use a Collator.

E.g

List<String> names = ... read names from db;
Collator collator = Collator.getInstance(new Locale("pt"));
Collections.sort(names, collator);

The names will then be sorted alphabetically. You may need to use collator.setStrength(SECONDARY) to get it to "ignore" differences due to accents. The behaviour is language specific so I can't say for sure.

like image 106
mdma Avatar answered Nov 18 '22 21:11

mdma


Pass a java.text.Collator to your string-sorting routine.

like image 2
dan04 Avatar answered Nov 18 '22 19:11

dan04