import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Test {
private static int chineseCompare(Object obj1,Object obj2){
Student s1 = (Student)obj1;
Student s2 = (Student)obj2;
Collator myCollator = Collator.getInstance(java.util.Locale.CHINA);
return myCollator.compare(s1.getName(), s2.getName());
}
public static void main(String[] args) {
//准备数据
List studentList = new ArrayList();
studentList.add(new Student(1,"张三"));
studentList.add(new Student(2,"李四"));
studentList.add(new Student(3,"王五"));
studentList.add(new Student(4,"赵六"));
studentList.add(new Student(5,"田七"));
studentList.add(new Student(6,"eee"));
studentList.add(new Student(7,"aaa"));
studentList.add(new Student(8,"ccc"));
//排序
Collections.sort(studentList,new Comparator() {
@Override
public int compare(Object o1, Object o2) {
return chineseCompare(o1,o2);
}
});
//输出
for(Student s:studentList){
System.out.println(s.getName());
}
}
}
class Student{
private int id;
private String name;
public Student(){}
public Student(int id,String name){
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
输出结果
aaa
ccc
eee
李四
田七
王五
张三
赵六
不知道为什么eee,aaa,ccc不参与排序,始终在最前面,不能混合排序