'Sorting arraylist in alphabetical order(Persian)

I have a list of items in Persian and I want to sort them in alphabetical order.
As I understood java does not support sorting in Persian alphabetical correctly.

My code:

List<String> items = new ArrayList<>();
items.add("آب");
items.add("بابا");
items.add("تار");
items.add("پارک");
items.add("توت");

Collections.sort(items);

When I print this list the result will be:

آب
بابا
تار
توت
پارک

But it must be like this:

آب
بابا
پارک
تار
توت

The problem is with these letters گ چ پ ژ
How can I fix it?



Solution 1:[1]

This code will give expected result:

import java.util.*;

public class Main {
final static String ORDER = "? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?";


public static void main(String[] args) {
    List<String> items = new ArrayList<String>();
    items.add("??");
    items.add("????");
    items.add("???");
    items.add("????");
    items.add("???");

    Collections.sort(items,  new Comparator<String>() {
        @Override
        public int compare(String o2, String o1) {
            return ORDER.indexOf(o2.charAt(0)) -  
            ORDER.indexOf(o1.charAt(0));
        }
    });

    for (String str : items) {
        System.out.println(str);
    }

}

}

It sort by first letter only. To order by other letters, the compare method should be improved accordingly.

Solution 2:[2]

In case other solutions such as above ones didn't work there, you can use this hack,

static private String prepareForArabicSort(String text) {
        return text
                .replaceAll("?", "?")
                .replaceAll("?", "?")
                .replaceAll("?", "??")
                .replaceAll("?", "??")
                .replaceAll("?", "??")
                .replaceAll("?", "??");
}

Arrays.sort(list, (l, r) -> {
    return prepareForArabicSort(l).compareTo(prepareForArabicSort(r));
});

Solution 3:[3]

Unfortunately this is an old (since Java 1.4), but still unresolved enhancement request. See bug-report JDK-6182989 - Collator should support the correct sort order for Persian. But may be you can just copy the proposed code for a PersianCollator given there, and then use it like this:

Collections.sort(items, PersianCollator.persianCollator());

Solution 4:[4]

Not sure what version of Java you are using, but you could use a simple RuleBasedCollator to switch the order of the few characters that need to be switched (the rest should follow Unicode order). There is a good example in the description of the class in the API documentation.

There is also a complete example in the Java Tutorials that show how to use the RuleBasedCollator you create.

Solution 5:[5]

if you have an arrayList of objects you can use this method

first create a collator:

val collator: Collator = Collator.getInstance(Locale("fa", "IR"))
collator.strength = Collator.PRIMARY

then your list via sortedBy like this:

val sortList = items.sortedWith(compareBy (collator){ it.name})

that items is your arrayList of objects and it.name is your comparator

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sergey Krasnikov
Solution 2
Solution 3
Solution 4 banncee
Solution 5 Meysam Hadigheh