Java – how to sort the list by string value (Persian letter)?
•
Android
Invalid Sign
This 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());
}
}
}
resolvent:
You can use the organizer to see:
Performing Locale-Independent Comparisons
Sorting arabic words in java
Or create your own comparator. Here's the document
Object Ordering
The reason for sorting these strings is that the strings are sorted using the utf-16 char table. Therefore, in utf-16, these characters are:
> م Arabic letter (U 0645) > Arabic letter keheh (U 06a9) > Photo Arabic letter Farsi Yeh (U 06cc)
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码