public int compare(Object o1, Object o2) { Student s1 = (Student) o1; Student s2 = (Student) o2; if (s1.getAge() > s2.getAge()) return 1; else if (s1.getAge() == s2.getAge()) { return 0; } return -1; }
public static void main(String[] args) { Student zlj = new Student("¶¡ÏþÓî", 21); Student dxy = new Student("ÕÔËÄ", 22); Student cjc = new Student("ÕÅÈý", 11); Student lgc = new Student("ÁõÎä", 19); List<Student> studentList = new ArrayList<Student>(); studentList.add(zlj); studentList.add(dxy); studentList.add(cjc); studentList.add(lgc); System.out.println("°´ÕÕÄêýgÅÅÐò£º"); Collections.sort(studentList, new SortByAge()); for (Student student : studentList) { System.out.println(student.getName() + " / " + student.getAge()); } System.out.println(" ========= "); System.out.println("°´ÕÕÐÕÃûÅÅÐò"); Collections.sort(studentList, new SortByName()); for (Student student : studentList) { System.out.println(student.getName() + " / " + student.getAge()); } }