java 类:
package com.cxcm.crm.util;
import java.util.Comparator;
import com.jyxin.reservation.wx.entity.TimeSlot;
public class OrderDate implements Comparator<Object>{
public int compare(Object arg0,Object arg1){
TimeSlot start1 = (TimeSlot)arg0;
TimeSlot start2 = (TimeSlot)arg1;
int flag = start1.getStartTime().compareTo(start2.getStartTime()); //这里按我实体中的开始时间属性进行比较
return flag;
}
}
测试
public Map<String, String> listByOffice(String officeId) {
List<TimeSlot> list = timeSlotService.listRule(officeId);
Map<String, String> map = new LinkedHashMap<String, String>();
//排序时间,自定义一个比较器
OrderDate sort = new OrderDate();
Collections.sort(list,sort); //利用Collections.sort()对list进行排序
for(TimeSlot slot : list) {
map.put(slot.getId(), slot.getStartTime().toString()+"-"+slot.getEndTime().toString());
//System.out.println(slot.getStartTime().toString());//结果按时间升序排列
}
return map;
}
实体类:
public class TimeSlot extends BasePo {
private static final long serialVersionUID = -992874651434384779L;
/**
* 开始时间
*/
@Column(name = "STARTTIME")
@Temporal(TemporalType.TIME)
private Date startTime;
/**
* 结束时间
*/
@Column(name = "ENDTIME")
@Temporal(TemporalType.TIME)
private Date endTime;
/**
* 描述
*/
@Column(name = "DESCRIPTION")
private String description;
}