Java – How do I sort a List by string value (Persian alphabet)Object?

How do I sort a List by string value (Persian alphabet)?… here is a solution to the problem.

How do I sort a List by string value (Persian alphabet)?

I

have a list of students with two fields (name and number) and I want to sort the list by name (Persian name), but when I use Collections.sort to sort the list with some Persian alphabet like “? ”&“? ”&“? ”…
The result is:
But it must be: “ь

“, “م”, “ک”

Here is my code :

 public class Student {
    private String name;
    private int number;

public String getName() {
        return name;
    }

public void setName(String name) {
        this.name = name;
    }

public int getNumber() {
        return number;
    }

public void setNumber(int number) {
        this.number = number;
    }

@Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", number=" + number +
                '}';
    }

}

The public section is mainly {

    public static void main(String[] args) {

List<Student> studentList = new ArrayList<Student>();

Student temp1 = new Student();
            temp1.setName("ی");
            temp1.setNumber(5);

Student temp2 = new Student();
            temp2.setName("م");
            temp2.setNumber(4);

Student temp3 = new Student();
            temp3.setName("ک");
            temp3.setNumber(3);

studentList.add(temp1);
            studentList.add(temp2);
            studentList.add(temp3);

 before sort
            System.out.println("before sort");
            for(Student student : studentList){
                    System.out.println("Student name: " + student.getName());
            }

Locale locale = new Locale("fa");

System.out.println("--------------------");
            System.out.println("Language: " + locale.getDisplayLanguage());
            System.out.println("--------------------");

if (studentList.size() > 0) {
                    Collections.sort(studentList, new Comparator<Student>() {
                            @Override
                            public int compare(final Student object1, final Student object2) {
                                    return Collator.getInstance(locale).compare(object1.getName(), object2.getName());
                            }
                    } );
            }

 after sort
            System.out.println("after sort");
            for(Student student : studentList){
                    System.out.println("Student name: " + student.getName());
            }

}

Solution

You can use the organizer and take a look at this:

Performing Locale-Independent Comparisons

Sorting arabic words in java

Or create your own comparator. Here is the documentation.

Object Ordering

The reason these strings are sorted in this way is that the strings are sorted using a UTF-16 character table. So in UTF-16, these characters are:

  • Arabic letter MEEM (U+0645).
  • The Arabic alphabet KEHEH (U+06A9).
  • International Arabic alphabet Persian YEH (U+06CC).

Related Problems and Solutions