public class Student { private int studentId; private String studentName; private int age; private String upDate;數組
public Student(int studentId, String studentName, int age, String date) { this.studentId = studentId; this.studentName = studentName; this.age = age; this.upDate = date; } public int getStudentId() { return studentId; } public void setStudentId(int studentId) { this.studentId = studentId; } public String getStudentName() { return studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getUpDate() { return upDate; } public void setUpDate(String upDate) { this.upDate = upDate; }
第一種:實例化一個比較器ide
public class test {this
public static void main(String[] args) { Comparator<Student> comparator = new Comparator<Student>() { public int compare(Student s1, Student s2) { // 排序:更新時間 -- 年齡 --姓名---學號 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); try { Date dt1 = format.parse(s1.getUpDate()); Date dt2 = format.parse(s2.getUpDate()); if (dt1.getTime() != dt2.getTime()) { if (dt1.getTime() > dt2.getTime()) { return 1; } } else if (s1.getAge() != s2.getAge()) { return s1.getAge() - s2.getAge(); } else if (!s1.getStudentName().equals(s2.getStudentName())) { return s1.getStudentName().compareTo(s2.getStudentName()); } else { return s1.getStudentId() - s2.getStudentId(); } } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return 0; } }; Student stu1 = new Student(1, "zhangsan", 28, "2017-11-01"); Student stu2 = new Student(2, "zhagnsan", 19, "2017-11-01"); Student stu3 = new Student(3, "wangwu", 19, "2017-11-02"); Student stu4 = new Student(4, "wangwu", 19, "2017-12-02"); Student stu5 = new Student(5, "zhaoliu", 18, "2017-12-08"); ArrayList<Student> list = new ArrayList<Student>(); list.add(stu1); list.add(stu2); list.add(stu3); list.add(stu4); list.add(stu5); // 這裏就會自動根據規則進行排序 Collections.sort(list, comparator); for (int i = 0; i < list.size(); i++) { Student stu = list.get(i); System.out.println("更新時間:" + stu.getUpDate() + " 年齡:" + stu.getAge() + " 姓名:" + stu.getStudentName() + " 學號:" + stu.getStudentId()); } }
}.net
第二種:實現Comparable 接口code
public class Student2 implements Comparable<Student2> { private int studentId; private String studentName; private int age; private String upDate;orm
public Student2(int studentId, String studentName, int age, String date) { this.studentId = studentId; this.studentName = studentName; this.age = age; this.upDate = date; } public int getStudentId() { return studentId; } public void setStudentId(int studentId) { this.studentId = studentId; } public String getStudentName() { return studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getUpDate() { return upDate; } public void setUpDate(String upDate) { this.upDate = upDate; } [@Override](https://my.oschina.net/u/1162528) public int hashCode() { return 0; } [@Override](https://my.oschina.net/u/1162528) public boolean equals(Object obj) { if (obj instanceof Student2) { Student2 stu = (Student2) obj; if ((age == stu.getAge()) && (studentName.equals(stu.getStudentName())) && (studentId == stu.getStudentId())) { return true; } else return true; } else { return false; } } [@Override](https://my.oschina.net/u/1162528) public int compareTo(Student2 o) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Date dt1; try { dt1 = format.parse(o.getUpDate()); Date dt2 = format.parse(upDate); if (dt1.getTime() != dt2.getTime()) { if (dt1.getTime() > dt2.getTime()) { return 1; } } else if (age != o.getAge()) { return age - o.getAge(); } else if (!studentName.equals(o.getStudentName())) { return studentName.compareTo(o.getStudentName()); } else { return studentId - o.getStudentId(); } } catch (ParseException e) { e.printStackTrace(); } return 0; }
}排序
public class test2 { /** * @param args */ public static void main(String[] args) { Student2 stu1 = new Student2(1, "zhangsan", 28, "2017-11-01"); Student2 stu2 = new Student2(2, "zhagnsan", 19, "2017-11-01"); Student2 stu3 = new Student2(3, "wangwu", 19, "2017-11-02"); Student2 stu4 = new Student2(4, "wangwu", 19, "2017-12-02"); Student2 stu5 = new Student2(5, "zhaoliu", 18, "2017-12-08");接口
ArrayList<Student2> list = new ArrayList<Student2>(); list.add(stu1); list.add(stu2); list.add(stu3); list.add(stu4); list.add(stu5); // 這裏就會自動根據規則進行排序 Collections.sort(list); for (int i = 0; i < list.size(); i++) { Student2 stu = list.get(i); System.out.println("更新時間:"+stu.getUpDate()+ " 年齡:" + stu.getAge() + " 姓名:" + stu.getStudentName() + " 學號:" + stu.getStudentId()); } }
}get
第三種:補充說明SortList,可做爲擴展hash
public class SortList<E> {
@SuppressWarnings({"unchecked", "rawtypes"}) public void Sort(List<E> list, final String method, final String sort) { Collections.sort(list, new Comparator() { public int compare(Object a, Object b) { int ret = 0; try { Method m1 = ((E) a).getClass().getMethod(method, null); Method m2 = ((E) b).getClass().getMethod(method, null); if (sort != null && "desc".equals(sort)) // 倒序 ret = m2.invoke(((E) b), null).toString().compareTo(m1.invoke(((E) a), null).toString()); else // 正序 ret = m1.invoke(((E) a), null).toString().compareTo(m2.invoke(((E) b), null).toString()); } catch (Exception ne) { System.out.println(ne); } return ret; } }); }
}
public class test {
public static void main(String[] args) { Student stu1 = new Student(1, "zhangsan", 28, "2017-11-01"); Student stu2 = new Student(2, "zhagnsan", 19, "2017-11-01"); Student stu3 = new Student(3, "wangwu", 19, "2017-11-02"); Student stu4 = new Student(4, "wangwu", 19, "2017-12-02"); Student stu5 = new Student(5, "zhaoliu", 18, "2017-12-08"); ArrayList<Student> list = new ArrayList<Student>(); list.add(stu1); list.add(stu2); list.add(stu3); list.add(stu4); list.add(stu5); SortList<Student> sortList = new SortList<Student>(); //可擴展成數組 sortList.Sort(list, "getUpDate", "asc"); for (int i = 0; i < list.size(); i++) { Student stu = list.get(i); System.out.println("更新時間:" + stu.getUpDate() + " 年齡:" + stu.getAge() + " 姓名:" + stu.getStudentName() + " 學號:" + stu.getStudentId()); } }
}