Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I sort List<Object> by String value (Persian Alphabet)?

Tags:

java

android

I have a List of Student with tow fields (name & number), I want to sort the List by name (Persian name) but When I sort the List with Collections.sort there is problem 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 +
                '}';
    }

}

public class Main {

    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());
            }

    }

}

like image 476
amin.mp Avatar asked Jun 12 '16 08:06

amin.mp


People also ask

How do you sort a list containing objects?

sort() method to sort a list of objects using some examples. By default, the sort() method sorts a given list into ascending order (or natural order). We can use Collections. reverseOrder() method, which returns a Comparator, for reverse sorting.

How do you sort a list alphabetically in darts?

List Contains method called sort, that function will sort the list in alphabetical order (from a to z). Technically you only need _myBranchListName. sort() to sort the array.

How can we sort a list of elements in Java?

Summary. Collections class sort() method is used to sort a list in Java. We can sort a list in natural ordering where the list elements must implement Comparable interface. We can also pass a Comparator implementation to define the sorting rules.


2 Answers

You can use a collator, take a look at this:

Performing Locale-Independent Comparisons

Sorting arabic words in java

Or create your own Comparator. Here the doc.

Object Ordering

The reason you are getting those strings sorted in that way is that strings are sorted using the UTF-16 char table. So in UTF-16 those characters are:

  • م ARABIC LETTER MEEM (U+0645)
  • ک ARABIC LETTER KEHEH (U+06A9)
  • ی ARABIC LETTER FARSI YEH (U+06CC)
like image 158
aldebaran-ms Avatar answered Sep 20 '22 07:09

aldebaran-ms


Use Collator with a new locate for "IR" :

Collator.getInstance(new Locale("fa", "IR")).compare ...
like image 32
Mahmoud_Mehri Avatar answered Sep 21 '22 07:09

Mahmoud_Mehri