方法一:將要排序的對象類實現Comparable<>接口。ide
public static void main(String[] args) { List<student> lists = Lists.newArrayList(); student student = new student(); student.setIdx(1); student.setName("張三1"); student.setSex("女1"); lists.add(student); student student2 = new student(); student2.setIdx(3); student2.setName("張三3"); student2.setSex("女3"); lists.add(student2); student student4 = new student(); student4.setIdx(5); student4.setName("張三5"); student4.setSex("女5"); lists.add(student4); student student3 = new student(); student3.setIdx(4); student3.setName("張三4"); student3.setSex("女4"); lists.add(student3); student student1 = new student(); student1.setIdx(2); student1.setName("張三2"); student1.setSex("女2"); lists.add(student1); Collections.sort(lists); System.out.println(lists.toString()); } static class student implements Comparable<student>{ Integer idx; String name; String sex; String phone; public Integer getIdx() { return idx; } public void setIdx(Integer idx) { this.idx = idx; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } @Override public String toString() { return "student{" + "idx=" + idx + ", name='" + name + '\'' + ", sex='" + sex + '\'' + ", phone='" + phone + '\'' + '}'; } @Override public int compareTo(student o) { return this.idx - o.idx; } }