码迷,mamicode.com
首页 > 其他好文 > 详细

compareTo & toString

时间:2016-07-28 15:05:43      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:

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为按照时间倒叙排序

 

compareTo & toString

标签:

原文地址:http://www.cnblogs.com/webglcn/p/5714536.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!