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 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. 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.

Sign up to request clarification or add additional context in comments.

2 Comments

@kerem why did you remove the attribution?
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.
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 Comments

What does Collator.PRIMARY will do ?
@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.
The collator IS a comparator. I just found out.
@JanDvorak Ohh Collator is a Comparator, which does support I18N .. :)
@RahulAgrawal I admit I shouldn't be surprised that a Collator is a Comparator :-)
|
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 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.