37

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 ?

3
  • look out for Comparable interface
    – Jayy
    Commented Oct 15, 2012 at 5: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
    Commented Oct 15, 2012 at 5:39
  • Sorting should consider base char , accent , case , bits. So output should be [abc, Abc, ABC, àbc, Àbc, äbc, Äbc] for FRANCE locale Commented Oct 15, 2012 at 5:43

3 Answers 3

64

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.

2
  • 2
    @kerem why did you remove the attribution? Commented Feb 18, 2016 at 9:46
  • Jan sorry if i did something wrong. Collator.PRIMARY is discussed in the next answer with details, and it is obvious that you get the idea from @Bhesh. Commented Feb 18, 2016 at 11:54
31

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);
6
  • 1
    What does Collator.PRIMARY will do ? Commented Oct 15, 2012 at 5:46
  • 1
    @RahulAgrawal: It's the strength of comparision. Please read the documentation, you will find a detailed explanation there. That's where I borrowed the example from. Commented Oct 15, 2012 at 5:50
  • 1
    The collator IS a comparator. I just found out. Commented Oct 15, 2012 at 5:53
  • @JanDvorak Ohh Collator is a Comparator, which does support I18N .. :) Commented Oct 15, 2012 at 6:02
  • @RahulAgrawal I admit I shouldn't be surprised that a Collator is a Comparator :-) Commented Oct 15, 2012 at 6:05
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());
        }
    });
1
  • 1
    While this code may solve the question, including an explanation 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
    Commented Apr 1, 2019 at 14:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.