gpt4 book ai didi

java - BasicDBObject 的自定义排序未按预期排序

转载 作者:行者123 更新时间:2023-12-01 10:59:37 24 4
gpt4 key购买 nike

在下面的代码中,我尝试按 BasicDBObject 列表的日期降序自定义排序。

打印的输出是:

{ "year" : "2015" , "month" : "12" , "day" : "1" , "time" : "07:30:59"}
{ "year" : "2015" , "month" : "12" , "day" : "1" , "time" : "07:30:20"}
{ "year" : "2015" , "month" : "12" , "day" : "1" , "time" : "07:30:00"}
{ "year" : "2015" , "month" : "01" , "day" : "23" , "time" : "07:30:59"}
{ "year" : "2015" , "month" : "09" , "day" : "26" , "time" : "07:30:59"}

什么时候应该是:

{ "year" : "2015" , "month" : "12" , "day" : "1" , "time" : "07:30:59"}
{ "year" : "2015" , "month" : "12" , "day" : "1" , "time" : "07:30:20"}
{ "year" : "2015" , "month" : "12" , "day" : "1" , "time" : "07:30:00"}
{ "year" : "2015" , "month" : "09" , "day" : "26" , "time" : "07:30:59"}
{ "year" : "2015" , "month" : "01" , "day" : "23" , "time" : "07:30:59"}

因为 9 月 (09) 发生在 1 月 (01) 之后。

排序比较方法有问题吗?

代码:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormatter;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;


public class SortByDate
{

private void testSort()
{

List<DBObject> l = new ArrayList<DBObject>();

DBObject dbo = new BasicDBObject();
dbo.put("year", "2015");
dbo.put("month", "12");
dbo.put("day", "1");
dbo.put("time", "07:30:20");
l.add(dbo);

dbo = new BasicDBObject();
dbo.put("year", "2015");
dbo.put("month", "09");
dbo.put("day", "26");
dbo.put("time", "07:30:59");
l.add(dbo);

dbo = new BasicDBObject();
dbo.put("year", "2015");
dbo.put("month", "12");
dbo.put("day", "1");
dbo.put("time", "07:30:59");
l.add(dbo);

dbo = new BasicDBObject();
dbo.put("year", "2015");
dbo.put("month", "01");
dbo.put("day", "23");
dbo.put("time", "07:30:59");
l.add(dbo);

dbo = new BasicDBObject();
dbo.put("year", "2015");
dbo.put("month", "12");
dbo.put("day", "1");
dbo.put("time", "07:30:00");

l.add(dbo);

DateTimeFormatter dtf = org.joda.time.format.DateTimeFormat.forPattern("MM/dd/yyyy HH:mm:ss");

Collections.sort(l, new Comparator<DBObject>()
{
@Override
public int compare(final DBObject o1, final DBObject o2)
{
long m1 = dtf.parseMillis((String) o1.get("month")+"/"+(String) o1.get("day")+"/"+(String) o1.get("year")+" "+(String) o1.get("time"));
long m2 = dtf.parseMillis((String) o2.get("month")+"/"+(String) o2.get("day")+"/"+(String) o2.get("year")+" "+(String) o2.get("time"));

return (int)m2 - (int)m1;
}
});

for(DBObject d : l){
System.out.println(d.toString());
}

}

public static void main(String args[])
{
new SortByDate().testSort();
}

}

最佳答案

问题出在这里:

return (int)m2 - (int)m1;

您将long millis转换为int,但值大于Integer.MAX_VALUE,因此发生溢出并且转换返回错误结果(它例如,可以为负数)。正确的转换可能是:

return (int)(m2 - m1);

但同样,如果日期相差很多,溢出仍然可能发生。正确且安全地执行您需要的操作的方法是使用 Long.compare :

return Long.compare(m2, m1);

关于java - BasicDBObject 的自定义排序未按预期排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33438851/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com