按工号和时间排序
package com;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<ConfigVO> list = new ArrayList<ConfigVO>();
ConfigVO a1 = new ConfigVO();
a1.setEmployeeNumber("1");
a1.setActualCheckInTime("2020-05-23");
ConfigVO a2 = new ConfigVO();
a2.setEmployeeNumber("2");
a2.setActualCheckInTime("2020-04-23");
ConfigVO a3 = new ConfigVO();
a3.setEmployeeNumber("1");
a3.setActualCheckInTime("2020-03-23");
ConfigVO a4 = new ConfigVO();
a4.setEmployeeNumber("2");
a4.setActualCheckInTime("2020-06-23");
list.add(a1);
list.add(a2);
list.add(a3);
list.add(a4);
// 第一种Collections.sort排序
Collections.sort(list,new Comparator<ConfigVO>() {
public int compare(ConfigVO o1,ConfigVO o2) {
if (o1.getEmployeeNumber().equals(o2.getEmployeeNumber())) {
return o1.getActualCheckInTime().compareTo(o2.getActualCheckInTime());
} else {
return o1.getEmployeeNumber().compareTo(o2.getEmployeeNumber());
}
}
});
System.out.println(list.toString());
// 第二种stream().sorted排序
list.stream().sorted(
(o1, o2) -> {
if (o1.getEmployeeNumber().equals(o2.getEmployeeNumber())) {
return o1.getActualCheckInTime().compareTo(o2.getActualCheckInTime());
} else {
return o1.getEmployeeNumber().compareTo(o2.getEmployeeNumber());
}
}
).forEach(System.out::println);
}
}
package com;
public class ConfigVO {
public String employeeNumber;
public String actualCheckInTime;
public String getEmployeeNumber() {
return employeeNumber;
}
public void setEmployeeNumber(String employeeNumber) {
this.employeeNumber = employeeNumber;
}
public String getActualCheckInTime() {
return actualCheckInTime;
}
public void setActualCheckInTime(String actualCheckInTime) {
this.actualCheckInTime = actualCheckInTime;
}
@Override
public String toString() {
return "ConfigVO{" +
"employeeNumber='" + employeeNumber + '\'' +
", actualCheckInTime='" + actualCheckInTime + '\'' +
'}';
}
}