根據List集合中的對象屬性排序
阿新 • • 發佈:2018-03-20
his return nts 類型 sin 屬性 ppr 圖片 compareto
首先創建一個Student對象,裏面有三個屬性,分別是int類型,String類型,Date類型
package com.sinoway.cisp.test; import java.text.SimpleDateFormat; import java.util.Date; public class Student { private int age; private String name; private Date birthday; public int getAge() { return age; } publicvoid setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) {this.birthday = birthday; } @Override public String toString() { return "Student [age=" + age + ", name=" + name + ", birthday=" + new SimpleDateFormat("yyyy-MM-dd").format(birthday) + "]"; // 註意,為了方便看,這裏將Date類型的birthday格式化為"yyyy-MM-dd"的String類型 } }
1、給int類型的age從小到大排序
package com.sinoway.cisp.test; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import org.junit.Test; public class ObjectSortTest { @SuppressWarnings("all") public static void intSort(List<Student> studentList) { Collections.sort(studentList, new Comparator() { @Override public int compare(Object o1, Object o2) { Student student1 = (Student) o1; Student student2 = (Student) o2; if (student1.getAge() > student2.getAge()) { return 1; } else if (student1.getAge() == student2.getAge()) { return 0; } else { return -1; } } }); } @Test public void intSortTest() throws Exception { Student student1 = new Student(); student1.setAge(18); student1.setName("C"); student1.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse("2018-03-20")); Student student2 = new Student(); student2.setAge(16); student2.setName("B"); student2.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse("2018-01-20")); Student student3 = new Student(); student3.setAge(20); student3.setName("A"); student3.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse("2018-02-20")); List<Student> studentList = new ArrayList<>(); studentList.add(student1); studentList.add(student2); studentList.add(student3); for (Student student : studentList) { System.out.println(student.toString()); } System.err.println("----------"); intSort(studentList); for (Student student : studentList) { System.out.println(student.toString()); } } }
輸出為:
2、給String類型的name排序
@SuppressWarnings("all") public static void intSort(List<Student> studentList) { Collections.sort(studentList, new Comparator() { @Override public int compare(Object o1, Object o2) { Student student1 = (Student) o1; Student student2 = (Student) o2; return student1.getName().compareTo(student2.getName()); } }); }
輸出為:
3、給Date類型的birthday排序
@SuppressWarnings("all") public static void intSort(List<Student> studentList) { Collections.sort(studentList, new Comparator() { @Override public int compare(Object o1, Object o2) { Student student1 = (Student) o1; Student student2 = (Student) o2; if (student1.getBirthday().getTime() > student2.getBirthday().getTime()) { return 1; } else if (student1.getBirthday().getTime() == student2.getBirthday().getTime()) { return 0; } else { return -1; } } }); }
輸出為:
根據List集合中的對象屬性排序