35

I want to sort below List of strings as per user locale

List<String> words = Arrays.asList(
      "Äbc", "äbc", "Àbc", "àbc", "Abc", "abc", "ABC"
    );

For different user locale sort output should be different as per there locale.

How to sort above list as per user locale ?

I tried

Collections.sort(words , String.CASE_INSENSITIVE_ORDER);

But this is not working for localization, so how to pass locale parameter to Collections.sort() or is there any other efficient way ?

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
Rahul Agrawal
  • 8,913
  • 18
  • 47
  • 59
  • look out for Comparable interface – Jayy Oct 15 '12 at 05:37
  • your output is [Abc, abc, ABC, Àbc, àbc, Äbc, äbc] after sorting. isn't this correct? i think sorting is already based on 1-alphabetical order 2-locale order. – Juvanis Oct 15 '12 at 05:39
  • Sorting should consider base char , accent , case , bits. So output should be [abc, Abc, ABC, àbc, Àbc, äbc, Äbc] for FRANCE locale – Rahul Agrawal Oct 15 '12 at 05:43

3 Answers3

63

You can use a sort with a custom Comparator. See the Collator interface

Collator coll = Collator.getInstance(locale);
coll.setStrength(Collator.PRIMARY);
Collections.sort(words, coll);

The collator is a comparator and can be passed directly to the Collections.sort(...) method.

Kerem Baydoğan
  • 10,475
  • 1
  • 43
  • 50
John Dvorak
  • 26,799
  • 13
  • 69
  • 83
29

I think this what you should be using - Collator

The Collator class performs locale-sensitive String comparison. You use this class to build searching and sorting routines for natural language text.

Do something as follows in your comparator -

public int compare(String arg1, Sting arg2) {
    Collator usCollator = Collator.getInstance(Locale.US); //Your locale here
    usCollator.setStrength(Collator.PRIMARY);
    return usCollator.compare(arg1, arg2);
}

And pass an instance of the comparator the Collections.sort method.

Update

Like @Jan Dvorak said, it actually is a comparator, so you can just create it's intance with the desired locale, set the strength and pass it the sort method:

Collactor usCollator = Collator.getInstance(Locale.US); //Your locale here
usCollator.setStrength(Collator.PRIMARY); //desired strength
Collections.sort(yourList, usCollator);
Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
9
List<MODEL> ulke = new ArrayList<MODEL>();

    Collections.sort(ulke, new Comparator<MODEL>() {
        Collator collator = Collator.getInstance(new Locale("tr-TR"));
        @Override
        public int compare(MODEL o1, MODEL o2) {
            return collator.compare(o1.getULKEAD(), o2.getULKEAD());
        }
    });
Mehmet Onar
  • 349
  • 2
  • 13
  • 1
    While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Suraj Rao Apr 01 '19 at 14:49