import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class GenericWork {
public static void main(String[] args) {
List<Employee> list = new ArrayList<>();
list.add(new Employee("Bill",5000,new Mydate(2004,3,9)));
list.add(new Employee("Satily",8000,new Mydate(2006,9,24)));
list.add(new Employee("Martin",11000,new Mydate(2001,5,2)));
System.out.println(list);
list.sort(new Comparator<Employee>() {
@Override//使用匿名内部类重写接口Compartor,传入字符就判断ASCII码值,数字就判断大小,再根据冒泡法排序
public int compare(Employee o1, Employee o2) {
if(!(o1 instanceof Employee && o2 instanceof Employee)) {
return 0;
}
if(o1.getName().compareTo(o2.getName()) != 0){
int i = o1.getName().compareTo(o2.getName());
return i;
}
if(o1.getBirthday().getYear() - o2.getBirthday().getYear() != 0){
int i1 = o1.getBirthday().getYear() - o2.getBirthday().getYear();
return i1;
}
if(o1.getBirthday().getMonth() - o2.getBirthday().getMonth() != 0){
return o1.getBirthday().getMonth() - o2.getBirthday().getMonth();
}
if(o1.getBirthday().getDay() - o2.getBirthday().getDay() != 0){
return o1.getBirthday().getDay() - o2.getBirthday().getDay();
}
return 0;
}
});
System.out.println(list);
}
}
class Employee{
private String name;
private double sal;
private Mydate birthday;
public Employee(String name, double sal, Mydate birthday) {
this.name = name;
this.sal = sal;
this.birthday = birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSal() {
return sal;
}
public void setSal(double sal) {
this.sal = sal;
}
public Mydate getBirthday() {
return birthday;
}
public void setBirthday(Mydate birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "\nEmployee{" +
"name='" + name + '\'' +
", sal=" + sal +
", birthday=" + birthday +
'}';
}
}
class Mydate{
private int year;
private int month;
private int day;
public Mydate(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
@Override
public String toString() {
return "Mydate{" +
"year=" + year +
", month=" + month +
", day=" + day +
'}';
}
}