1、实体类实现comparable接口,重写compareTo方法
package io;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Student implements Comparable{
private String name;
private int age;
private Date birthDay;
public Student(String name, int age, String birthDay) {
super();
this.name = name;
this.age = age;
DateFormat ds=new SimpleDateFormat("yyyy-MM-dd");
try {
this.birthDay = ds.parse(birthDay);
} catch (ParseException e) {
e.printStackTrace();
};
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBirthDay() {
return birthDay;
}
public void setBirthDay(Date birthDay) {
this.birthDay = birthDay;
}
//按名字降序 按生日升序
@Override
public int compareTo(Student o) {
int result=0;
result=-this.name.compareTo(o.name);
if(result==0){
result=this.birthDay.compareTo(o.birthDay);
}
return result;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", birthDay=" + birthDay + "]";
}
}
package io;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class App {
public static void main(String[] args) {
List list=new ArrayList();
list.add(new Student("张三", 20, "1987-10-5"));
list.add(new Student("王五", 22, "1986-10-5"));
list.add(new Student("赵六", 25, "1988-10-5"));
list.add(new Student("王七", 19, "1980-10-5"));
Collections.sort(list);
for(Student s:list){
System.out.println(s);
}
}
}
2、创建新的业务类实现comparator接口,重写compare方法。
package io;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Student {
private String name;
private int age;
private Date birthDay;
public Student(String name, int age, String birthDay) {
super();
this.name = name;
this.age = age;
DateFormat ds=new SimpleDateFormat("yyyy-MM-dd");
try {
this.birthDay = ds.parse(birthDay);
} catch (ParseException e) {
e.printStackTrace();
};
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBirthDay() {
return birthDay;
}
public void setBirthDay(Date birthDay) {
this.birthDay = birthDay;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", birthDay=" + birthDay + "]";
}
}
package io;
import java.util.Comparator;
public class BirthdayComp implements Comparator{
@Override
public int compare(Student o1, Student o2) {
return o1.getBirthDay().compareTo(o2.getBirthDay());
}
}
package io;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class App {
public static void main(String[] args) {
List list=new ArrayList();
list.add(new Student("张三", 20, "1987-10-5"));
list.add(new Student("王五", 22, "1986-10-5"));
list.add(new Student("赵六", 25, "1988-10-5"));
list.add(new Student("王七", 19, "1980-10-5"));
Collections.sort(list, new BirthdayComp());
for(Student s:list){
System.out.println(s);
}
}
}