首先创建一个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;
} public void 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 stringSort(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 dateSort(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;
}
}
});
}
输出为: