Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JAVA:如何根据字段对列表进行排序_Java_Sorting_Date_Time - Fatal编程技术网

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

JAVA:如何根据字段对列表进行排序,java,sorting,date,time,Java,Sorting,Date,Time,我有下面的列表,我想根据日期对整个列表进行排序,我该怎么做 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

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

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);
尝试使用以下方法对其进行排序:


devicePing.sort(Comparator.comparing(devicePing::getDateTime))

从最旧到最新排序?从最新到最旧使用比较器-而且您没有显示完整的代码。请阅读并尝试使用具有相同代码的LocalDateTime。
public void calculateTimesheet(RiderLocation RiderLocation,List devicePing,List riderAvailability){devicePing.sort(Comparator.comparing(devicePing::getDate));for(devicePing dp:devicePing){System.out.println(“dp:+dp”)}
我尝试了您的示例,但它打印的是相同的数据,而不是经过排序的数据
DevicePing::getDateTime
,您的所有日期都在同一天。使用日期和时间实际上它是一个时间戳请检查我编辑的问题Java类型是什么<代码>本地日期时间
<代码>日期?我想根据时间戳对列表进行排序
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);
        }
    }
@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
    }