Reputation: 109
I have an ArrayList of custom objects that I want to sort alphabetically. The problem is that the field I want to sort it by sometimes contains non-english characters, like á
or é
.
I wanted to do this by using Collections.sort()
, but with this method, the items with non-english strings in their fields don't get sorted correctly.
This is what I've tried:
public List<Video> sortDatabase(List<Video> videos){
List<Video> sortedList = new ArrayList<>(videos);
Collections.sort(sortedList, new Comparator<Video>() {
@Override
public int compare(Video video, Video t1) {
return video.getTitle().compareTo(t1.getTitle());
}
});
return orderedList;
}
EDIT
I am using Android Studio with minSdkVersion 21
Upvotes: 0
Views: 171
Reputation: 6188
but with this method, the items with non-english (sic) strings in their fields don't get sorted correctly.
Are you sure that's the problem? Could it be that maybe you do not understand what is involved in sorting? I am pretty sure that the problem is the Unicode values of the characters. Characters are organized in character sets. The entire English language characters have a Unicode value lower or higher than characters from other languages, thus giving the impression that the sorting went wrong.
Maybe you should take a look at the Unicode chart to fully understand what I am saying.
Upvotes: 0
Reputation: 109
I've found a good answer here. This is how it looks like in my case:
public List<Video> sortDatabase(List<Video> videos){
List<Video> sortedList = new ArrayList<>(videos);
orderedList.sort(new VideoComparator());
return orderedList;
}
private static class VideoComparator implements Comparator<Video>{
Collator spCollator = Collator.getInstance(new Locale("hu", "HU"));
public int compare (Video e1, Video e2){
return spCollator.compare(e1.getTitle(), e2.getTitle());
}
}
In my case, the Locale had to be constructed with ("hu", "HU")
. You can check all supported languages here
This solution works fine, however, I am trying to use this for an Android application where this method only works with API level 24 or higher. So if anybody has a different solution, I will make that the accepted answer.
Upvotes: 1