Comparator 比较两个对象排序

List<Object> objList = new ArrayList<Object>();
	List<MsgBO> list = service
			.getMsgBOList();
		for (MsgBO smBO : list) {
			objList.add(smBO);
		}

		List<SmsBO> logs = service
				.getSmsBOList();
		for (SmsBO log : logs) {
		     objList.add(log);
		}

		// 排序
		Collections.sort(objList, new Comparator<Object>() {
			@Override
			public int compare(Object o1, Object o2) {
				Date d1 = null, d2 = null;
				if (o1 instanceof MsgBO) {
					d1 = ((MsgBO) o1).getCreateTime();
				} else if (o1 instanceof SmsBO) {
					d1 = ((SmsBO) o1).getCreateTime();
				}

				if (o2 instanceof MsgBO) {
					d2 = ((MsgBO) o2).getCreateTime();
				} else if (o2 instanceof SmsBO) {
					d2 = ((SmsBO) o2).getCreateTime();
				}

				return d1.compareTo(d2);
			}
		});
	

解析代码
for(Object  data : objList ){
if (data instanceof MsgBO ) 
MsgBO msgBO = (MsgBO ) data;
else 
SmsBO smsBO = (SmsBO) data;
}




// 排序
		Collections.sort(resultBos, new Comparator<Object>() {
			@Override
			public int compare(Object o1, Object o2) {
				Date d1 = null, d2 = null;
				d1 = ((MsgBO) o1).getTime();
				d2 = ((MsgBO) o2).getTime();
				return d1.compareTo(d2);
			}
		});


你可能感兴趣的:(comparator)