compareTo & toString

public class UnAssignedRecord implements Comparable<UnAssignedRecord> {
    private String time; //hour:min:sec

    public String getTime() {
        return time;
    }
    public void setTime(String time) {
        this.time = time;
    }
    
    @Override
    public String toString() {
        return "UnAssignedRecord [id=" + id + ", tableIndex=" + tableIndex + ", name=" + name + ", content=" + content + ", time=" + time + "]";
    }
    
    @Override
    public int compareTo(UnAssignedRecord arg) {
        UnAssignedRecord record = (UnAssignedRecord)arg;
        String o1 = record.getTime();
        String o2 = this.getTime();
        
        Date t1 = SiZhenUtil.convertDateTimeNoMilSec(o1);
        Date t2 = SiZhenUtil.convertDateTimeNoMilSec(o2);
        
        return (t1.getTime() > t2.getTime()) ? 1 : (t1.getTime() == t2.getTime() ? 0 : -1);
    }    
}

 

List<UnAssignedRecord> a = new ArrayList<UnAssignedRecord>();
a.add(new UnAssignedRecord());
a.add(new UnAssignedRecord());
a.add(new UnAssignedRecord());

Collections.sort(a);

// 此时a为按照时间倒叙排序

 

posted @ 2016-07-28 13:56  webglcn  阅读(186)  评论(0编辑  收藏  举报