java中重写Comparator对两个list集合排序
public class test{
public static void main(String[] args) {
List<LeaveRequest> LvRequestList=new List<LeaveRequest>();
List<OtRequest> otRequestList=new List<OtRequest>();
List allList=new List();
allList.addAll( LvRequestList);
allList.addAll(otRequestList);
RequestComparator comparator=new RequestComparator();
Collections.sort(list,comparator);
//这样就对两个装有不同类型的list按时间进行了排序
}
}
//Comparator
package com.hkairport.oaa.service.report;
import java.util.Comparator;
import java.util.Date;
import com.hkairport.oaa.model.application.LeaveRequest;
import com.hkairport.oaa.model.application.OtRequest;
public class RequestComparator implements Comparator {
public int compare(Object o1, Object o2) {
return getDate(o1).compareTo(getDate(o2));
}
public Date getDate(Object o) {
Date date = null;
if (o instanceof OtRequest) {
date = ((OtRequest) o).getOtDateFrom();
}
if (o instanceof LeaveRequest) {
date = ((LeaveRequest) o).getLeaveDateFrom();
}
return date;
}
}