当前位置: 代码迷 >> J2SE >> java的Comparable不支持中文吗,该怎么处理
  详细解决方案

java的Comparable不支持中文吗,该怎么处理

热度:52   发布时间:2016-04-24 02:14:01.0
java的Comparable不支持中文吗
Java code
import java.util.ArrayList;import java.util.Collections;public class StudentRanging {    public static void main(String[] args) {        ArrayList<Student> list = new ArrayList<Student>();        Student a = new Student("胖胖", 30, 90.5);        Student b = new Student("刘静", 25, 92.5);        Student c = new Student("苏凯多多", 21, 80.5);        Student f = new Student("阿成才", 24, 80.5);        Student d = new Student("许三多", 28, 90.5);        Student e = new Student("成才", 20, 90.5);        list.add(a);        list.add(b);        list.add(c);        list.add(d);        list.add(e);        list.add(f);        Collections.sort(list);        for (Student s : list) {            System.out.println("info:" + s);        }    }    public static class Student implements Comparable<Student> {        String name;        int age;        double score;        public Student(String name, int age, double score) {            this.name = name;            this.age = age;            this.score = score;        }        public String getName() {            return name;        }        public int getAge() {            return age;        }        public double getScore() {            return score;        }        public void setName(String name) {            this.name = name;        }        public void setAge(int age) {            this.age = age;        }        public void setScore(double score) {            this.score = score;        }        @Override        public String toString() {            return name + " " + age + " " + score;        }        @Override        public int compareTo(Student o) {            int result = 0;            return this.getName().compareTo(o.getName());        }    }}


------解决方案--------------------
这是根据汉字的unicode值来排序的。你要按照汉字拼音的首字母来排序:
比如:阿成才,成才,刘静,胖胖,苏凯多多,许三多
那复杂些看这篇博客
  相关解决方案