java中重寫Comparator對兩個list集合排序

public class test{  java

  public static void main(String[] args) {  app

  List<LeaveRequest>  LvRequestList=new List<LeaveRequest>();spa

  List<OtRequest> otRequestList=new List<OtRequest>();排序

  List   allList=new List();get

  allList.addAll( LvRequestList);
  allList.addAll(otRequestList);io

  RequestComparator comparator=new RequestComparator();
  Collections.sort(list,comparator);class

  //這樣就對兩個裝有不一樣類型的list按時間進行了排序test

 

  }import

}date

 

//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;

  }}

相關文章
相關標籤/搜索