java 按日期降序比较器排序未按预期工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14125298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 15:09:23  来源:igfitidea点击:

Sort by date descending comparator not working as expected

javasortingcomparator

提问by maple_shaft

Trying to make sense of the following output:

试图理解以下输出:

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);
    }
}

The method getDate() returns a java.sql.Timestamp.

getDate() 方法返回一个 java.sql.Timestamp。

Here is the output before the sort:

这是排序前的输出:

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

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
2016-101-02
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-103:03:03
2012-12-28 13:49:21.0

And after:

之后:

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

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-103:00.0
2012-10-08 13:02:02.0
2012年11月26日10:02:05.0
2012年10月7日03:02:01.0
2012年10月3日10:02:02.0
2012-12-28 13:49:21.0
2012-12-28 11:03:00.0

Any ideas why the bottom two objects might not be sorted correctly? I am using the MySQL JDBC implementation of this Timestamp.

为什么底部两个对象可能无法正确排序的任何想法?我正在使用此时间戳的 MySQL JDBC 实现。

回答by James McMinn

The difference between the last 2 dates and earlier dates will overflow integer.

最后 2 个日期和更早日期之间的差异将溢出整数。

Perhaps a better solution would be to compare the values, rather than subtract them.

也许更好的解决方案是比较这些值,而不是减去它们。

    long t1 = comm1.getDate().getTime();
    long t2 = comm2.getDate().getTime();
    if(t2 > t1)
            return 1;
    else if(t1 > t2)
            return -1;
    else
            return 0;

回答by Peter Lawrey

You can use

您可以使用

return Long.compare(t2, t1);

but you are better off comparing the dates.

但你最好比较日期。

return comm2.getDate().compareTo(comm1.getDate());

回答by gogognome

If the difference is bigger than about 25 days, an overflow occurs. (An int cannot represent a bigger time difference in milliseconds than about 25 days). This will make the comparison incorrect.

如果差异大于大约 25 天,则会发生溢出。(int 不能表示比大约 25 天更大的毫秒时间差)。这将使比较不正确。

This can be solved by changing the return statement into:

这可以通过将 return 语句更改为:

return Long.signum(t2 - t1);

回答by MrSmith42

My first idea is that the problem is an overflow. t1and t2are longs. The different may not fit in an int.

我的第一个想法是问题是溢出。 t1并且t2longs。不同的可能不适合 int。

I would check that.

我会检查的。

If compare on second-level is good enough for you, you should try:

如果二级比较对你来说已经足够了,你应该尝试:

return (int) ((t2 - t1)/1000);

This does not guarantee that there will be no overflows. I would at least add a test.

这并不能保证不会出现溢出。我至少会添加一个测试。

I think the best answer is not mine. My favorite is:

我认为最好的答案不是我的。我最喜欢的是:

    if(t2 > t1)
        return 1;
    else if(t1 > t2)
        return -1;
    else
        return 0;