Sort List of Strings with Localization
Asked Answered
A

3

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 ?

Algarroba answered 15/10, 2012 at 5:28 Comment(3)
look out for Comparable interfaceChadd
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.Linalool
Sorting should consider base char , accent , case , bits. So output should be [abc, Abc, ABC, àbc, Àbc, äbc, Äbc] for FRANCE localeAlgarroba
C
65

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.

Convex answered 15/10, 2012 at 5:40 Comment(2)
@kerem why did you remove the attribution?Convex
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.Providential
T
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);
Troxell answered 15/10, 2012 at 5:41 Comment(6)
What does Collator.PRIMARY will do ?Algarroba
@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.Troxell
The collator IS a comparator. I just found out.Convex
@JanDvorak Ohh Collator is a Comparator, which does support I18N .. :)Algarroba
@RahulAgrawal I admit I shouldn't be surprised that a Collator is a Comparator :-)Convex
The Comparator with Locale.GERMAN doesn't sort correctly. Our rules are somewhat complicated, but nonetheless.Guttersnipe
D
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());
        }
    });
Danikadanila answered 1/4, 2019 at 14:11 Comment(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.Footless

© 2022 - 2024 — McMap. All rights reserved.