gpt4 book ai didi

JAVA:如何根据字段对列表进行排序

转载 作者:行者123 更新时间:2023-11-29 04:23:56 25 4
gpt4 key购买 nike

<分区>

我有以下列表,我想根据日期对整个列表进行排序,我该怎么做?

List<DevicePing> devicePing=Arrays.asList(
new DevicePing(1L,2017-11-21 10:10:10.0),
new DevicePing(2L,2017-11-21 10:00:00.0),
new DevicePing(3L,2017-11-21 10:15:10.0),
new DevicePing(4L,2017-11-21 09:30:10.0),
new DevicePing(5L,2017-11-21 09:45:10.0),
new DevicePing(6L,2017-11-21 09:50:10.0),
new DevicePing(7L,2017-11-21 09:55:10.0);

当我使用下面的代码时,它在 o1.getDateTime() 处给我一个 nullPointerException

Collections.sort(myList, new Comparator<MyObject>() {
public int compare(MyObject o1, MyObject o2) {
return o1.getDateTime().compareTo(o2.getDateTime());
}
});

已编辑

现在我正在使用下面的代码,但它打印了相同的数据而不是已排序的数据

public void calculateTimesheet(RiderLocation riderLocation,List<DevicePing> devicePing,List<RiderAvailability> riderAvailability)
{
devicePing.sort(Comparator.comparing(DevicePing::getDate));
for(DevicePing dp:devicePing)
{
System.out.println("dp: " + dp);
}
}

DevicePing.java

@Entity
public class DevicePing {

public DevicePing(Long id ,Date createdAt) {
this.id = id;
this.createdAt = createdAt;
}

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne
private User rider;

@Column( nullable = false,columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
@Temporal(TemporalType.TIMESTAMP)
private Date date;

//getter nad setter
}

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