简易版:传送门:过关展将之——birthday排序(年月日排序)-CSDN博客
实现过程;
public static void main(String[] args) {
ArrayList<Employee> arrayList = new ArrayList<>();
Employee grace = new Employee("Grace", 12000, new MyDate(2012, 12, 12));
Employee tom = new Employee("Tom", 28000, new MyDate(2001, 12, 03));
Employee david = new Employee("David", 30000, new MyDate(1999, 10, 10));
Employee david1 = new Employee("David", 31000, new MyDate(2001, 11, 15));
Employee david2 = new Employee("David", 32000, new MyDate(1899, 5, 14));
Employee david3 = new Employee("David", 33000, new MyDate(2013, 04, 11));
Employee david4 = new Employee("David", 34000, new MyDate(1987, 04, 10));
Employee david5 = new Employee("David", 34000, new MyDate(1987, 04, 11));
arrayList.add(grace);
arrayList.add(tom);
arrayList.add(david);
arrayList.add(david1);
arrayList.add(david2);
arrayList.add(david3);
arrayList.add(david4);
arrayList.add(david5);
// System.out.println("打印先前数据:" + arrayList);
//现在进行排序
arrayList.sort(new Comparator<Employee>() {
@Override
public int compare(Employee o1, Employee o2) {
if (!(o1.getName() != null && o2.getName() != null)) {
throw new RuntimeException("员工姓名不能为空!");
}else{
if (o1.getName().equals(o2.getName())) {
if (o1.getBirthday().getYear() == o2.getBirthday().getYear()) {
if (o1.getBirthday().getMonth() == o2.getBirthday().getMonth()) {
if (o1.getBirthday().getDay() == o2.getBirthday().getDay()) {
return 0;
} else if (o1.getBirthday().getDay() > o2.getBirthday().getDay()) {
return 1;
} else {
return -1;
}
} else if (o1.getBirthday().getMonth() > o2.getBirthday().getMonth()) {
return 1;
} else {
return -1;
}
} else if (o1.getBirthday().getYear() > o2.getBirthday().getYear()) {
return 1;
} else {
return -1;
}
} else {
return o1.getName().compareTo(o2.getName());
}
}
}
});
System.out.println("========排序后======");
for (Object o :arrayList) {
Employee employee = (Employee) o;
System.out.println(employee);
}
}
}
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 +
'}';
}
}
class Employee {
private String name;
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", sal=" + sal +
", birthday=" + birthday +
'}';
}
private double sal;
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;
}
private MyDate birthday;
public Employee(String name, double sal, MyDate birthday) {
this.name = name;
this.sal = sal;
this.birthday = birthday;
}
}
以上代码过于繁琐,使用 if 的嵌套,可读性不高,现在进行改良:
进一步优化过程:
@SuppressWarnings({"all"})
public static void main(String[] args) {
ArrayList<EmployeeImp> arrayList = new ArrayList<>();
EmployeeImp grace = new EmployeeImp("Grace", 12000, new MyDateImp(2012, 12, 12));
EmployeeImp tom = new EmployeeImp("Tom", 28000, new MyDateImp(2001, 12, 03));
EmployeeImp david = new EmployeeImp("David", 30000, new MyDateImp(1999, 10, 10));
EmployeeImp david1 = new EmployeeImp("David", 31000, new MyDateImp(2001, 11, 15));
EmployeeImp david2 = new EmployeeImp("David", 32000, new MyDateImp(1899, 5, 14));
EmployeeImp david3 = new EmployeeImp("David", 33000, new MyDateImp(2013, 04, 11));
EmployeeImp david4 = new EmployeeImp("David", 34000, new MyDateImp(1987, 04, 10));
EmployeeImp david5 = new EmployeeImp("David", 34000, new MyDateImp(1987, 04, 11));
arrayList.add(grace);
arrayList.add(tom);
arrayList.add(david);
arrayList.add(david1);
arrayList.add(david2);
arrayList.add(david3);
arrayList.add(david4);
arrayList.add(david5);
//下面开始订制排序
arrayList.sort(new Comparator<EmployeeImp>() {
@Override
public int compare(EmployeeImp o1, EmployeeImp o2) {
if (!(o1 instanceof EmployeeImp && o2 instanceof EmployeeImp)) {
System.out.println("类型不匹配");
return 0;
}
//比较name
int i = o1.getName().compareTo(o2.getName());
if (i==0) {//如果名字相同,就继续比较
int yearMinus =o1.getBirthday().getYear() - o2.getBirthday().getYear();
if(yearMinus==0){//如果两个年份相同,继续比较月份
int monthMinus = o1.getBirthday().getMonth()- o2.getBirthday().getMonth();
if(monthMinus==0){//如果两个月份相同,继续比较日
int dayMinus = o1.getBirthday().getDay()- o2.getBirthday().getDay();
if(dayMinus==0){
return 0;
}else{
return dayMinus;
}
}else{
return monthMinus;
}
}else{
return yearMinus;
}
} else{//如果名字不相同,就返回,出结果
return i;
}
}
});
System.out.println("======定制排序后=====");
for (Object o :arrayList) {
EmployeeImp employeeImp = (EmployeeImp) o;
System.out.println(employeeImp);
}
}
}
class MyDateImp {
private int year;
private int month;
private int day;
public MyDateImp(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 +
'}';
}
}
class EmployeeImp {
private String name;
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", sal=" + sal +
", birthday=" + birthday +
'}';
}
private double sal;
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 MyDateImp getBirthday() {
return birthday;
}
public void setBirthday(MyDateImp birthday) {
this.birthday = birthday;
}
private MyDateImp birthday;
public EmployeeImp(String name, double sal, MyDateImp birthday) {
this.name = name;
this.sal = sal;
this.birthday = birthday;
}
}
使用过关斩将法改良:
public static void main(String[] args) {
ArrayList<EmployeeImp> arrayList = new ArrayList<>();
EmployeeImp grace = new EmployeeImp("Grace", 12000, new MyDateImp(2012, 12, 12));
EmployeeImp tom = new EmployeeImp("Tom", 28000, new MyDateImp(2001, 12, 03));
EmployeeImp david = new EmployeeImp("David", 30000, new MyDateImp(1999, 10, 10));
EmployeeImp david1 = new EmployeeImp("David", 31000, new MyDateImp(2001, 11, 15));
EmployeeImp david2 = new EmployeeImp("David", 32000, new MyDateImp(1899, 5, 14));
EmployeeImp david3 = new EmployeeImp("David", 33000, new MyDateImp(2013, 04, 11));
EmployeeImp david4 = new EmployeeImp("David", 34000, new MyDateImp(1987, 04, 10));
EmployeeImp david5 = new EmployeeImp("David", 34000, new MyDateImp(1987, 04, 11));
arrayList.add(grace);
arrayList.add(tom);
arrayList.add(david);
arrayList.add(david1);
arrayList.add(david2);
arrayList.add(david3);
arrayList.add(david4);
arrayList.add(david5);
//下面开始订制排序
arrayList.sort(new Comparator<EmployeeImp>() {
@Override
public int compare(EmployeeImp o1, EmployeeImp o2) {
if (!(o1 instanceof EmployeeImp && o2 instanceof EmployeeImp)) {
System.out.println("类型不匹配");
return 0;
}
//比较name
int i = o1.getName().compareTo(o2.getName());
if (i != 0) {//如果名字不相同,直接返回
return i;
}
//以下是对birthday的比较,因此,我们最好把这个比较,放在MyDate
int yearMinus = o1.getBirthday().getYear() - o2.getBirthday().getYear();
if (yearMinus != 0) {//如果两个年份不相同,直接返回
return yearMinus;
}
int monthMinus = o1.getBirthday().getMonth() - o2.getBirthday().getMonth();
if (monthMinus != 0) {//如果两个月份不相同,直接返回
return monthMinus;
}
//如果year、month都相同
return o1.getBirthday().getDay() - o2.getBirthday().getDay();
}
});
System.out.println("======定制排序后=====");
for (
Object o : arrayList) {
EmployeeImp employeeImp = (EmployeeImp) o;
System.out.println(employeeImp);
}
}
}
class MyDateImp {
private int year;
private int month;
private int day;
public MyDateImp(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 +
'}';
}
}
class EmployeeImp {
private String name;
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", sal=" + sal +
", birthday=" + birthday +
'}';
}
private double sal;
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 MyDateImp getBirthday() {
return birthday;
}
public void setBirthday(MyDateImp birthday) {
this.birthday = birthday;
}
private MyDateImp birthday;
public EmployeeImp(String name, double sal, MyDateImp birthday) {
this.name = name;
this.sal = sal;
this.birthday = birthday;
}
}
打印结果:
简易版:传送门:过关展将之——birthday排序(年月日排序)-CSDN博客