我有一个包含两个字段(姓名和编号)的学生列表,我想按姓名(波斯语姓名)对列表进行排序,但是当我使用 Collections.sort 对列表进行排序时出现问题与一些波斯字母表,如“?”&“?”&“?”… 结果是: 但它必须是: "ь", "م", "ک"
这是我的代码:
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)课主要{
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());
}
}
我是一名优秀的程序员,十分优秀!