首先按照员工类的名字进行排序,如果名字一样的话就按照出生日期排序,具体使用案例如下:
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
@SuppressWarnings("all")
public class genericTest02 {
public static void main(String[] args) {
Empoloyee xaoLi = new Empoloyee("小李子",12,new MyData(12,31,2000));
Empoloyee xaoZhang = new Empoloyee("小张打",10,new MyData(12,3,2002));
Empoloyee xaoWang = new Empoloyee("小王发",11,new MyData(7,31,1999));
ArrayList<Empoloyee> empoloyees = new ArrayList<>();
empoloyees.add(xaoLi);
empoloyees.add(xaoZhang);
empoloyees.add(xaoWang);
empoloyees.sort(new Comparator<Empoloyee>() {
@Override
public int compare(Empoloyee o1, Empoloyee o2) {
if(o1.getName().length() != o2.getName().length()){
return o1.getName().length() - o2.getName().length();
}
if(o1.getBrithday().getYear() != o2.getBrithday().getYear()){
return o1.getBrithday().getYear() - o2.getBrithday().getYear();
}
if(o1.getBrithday().getMonth() != o2.getBrithday().getMonth()){
return o1.getBrithday().getMonth() - o2.getBrithday().getMonth();
}
if(o1.getBrithday().getDay() != o2.getBrithday().getDay()){
return o1.getBrithday().getDay() - o2.getBrithday().getDay();
}
return 0;
}
});
Iterator<Empoloyee> iterator = empoloyees.iterator();
while (iterator.hasNext()) {
Empoloyee next = iterator.next();
System.out.println(next);
}
}
}
class Empoloyee {
private String name;
private double salary;
private MyData brithday;
public Empoloyee(String name, double salary, MyData brithday) {
this.name = name;
this.salary = salary;
this.brithday = brithday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public MyData getBrithday() {
return brithday;
}
public void setBrithday(MyData brithday) {
this.brithday = brithday;
}
@Override
public String toString() {
return "Empoloyee{" +
"name='" + name + '\'' +
", salary=" + salary +
", brithday='" + brithday + '\'' +
'}';
}
}
@SuppressWarnings("all")
class MyData{
private int month;
private int day;
private int year;
public MyData(int month, int day, int year) {
this.month = month;
this.day = day;
this.year = year;
}
@Override
public String toString() {
return "MyData{" +
"month=" + month +
", day=" + day +
", 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;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
}