- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
试图理解以下输出:
public class CommunicationComparator implements Comparator<Communication> {
@Override
public int compare(Communication comm1, Communication comm2) {
long t1 = comm1.getDate().getTime();
long t2 = comm2.getDate().getTime();
return (int) (t2 - t1);
}
}
getDate() 方法返回一个 java.sql.Timestamp。
这是排序前的输出:
for (Communication n : retVal) {
System.out.println(n.getDate().toString());
}
2012-10-03 10:02:02.0
2012-10-07 03:02:01.0
2012-10-08 13:02:02.0
2012-10-09 03:02:00.0
2012-11-26 10:02:05.0
2012-11-28 11:28:11.0
2012-12-03 12:03:01.0
2012-12-06 15:03:01.0
2012-12-13 14:03:00.0
2012-12-28 11:03:00.0
2012-12-28 13:49:21.0
之后:
Collections.sort(retVal, new CommunicationsComparator());
2012-12-13 14:03:00.0
2012-12-06 15:03:01.0
2012-12-03 12:03:01.0
2012-11-28 11:28:11.0
2012-10-09 03:02:00.0
2012-10-08 13:02:02.0
2012-11-26 10:02:05.0
2012-10-07 03:02:01.0
2012-10-03 10:02:02.0
2012-12-28 13:49:21.0
2012-12-28 11:03:00.0
知道为什么底部的两个对象可能无法正确排序吗?我正在使用此时间戳的 MySQL JDBC 实现。
最佳答案
最近 2 个日期和更早日期之间的差异将溢出整数。
也许更好的解决方案是比较值,而不是相减。
long t1 = comm1.getDate().getTime();
long t2 = comm2.getDate().getTime();
if(t2 > t1)
return 1;
else if(t1 > t2)
return -1;
else
return 0;
关于java - 按日期降序排序比较器未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14125298/
序 大家好呀,我是summo,这次来写写我在上班空闲(摸鱼)的时候做的一个小网站的事。去年阿里云不是推出了个活动嘛,2核2G的云服务器一年只要99块钱,懂行的人应该知道这个价格在业界已经是非常良心了
我尝试根据给定的级别顺序(BFS 顺序)构造 BST。我知道这是可能的,但我不知道我该怎么写。问题是我必须使用 BFS 序列。所以,我不能在这里使用递归,我必须迭代地编写我的程序......我发现这有
我是一名优秀的程序员,十分优秀!