简体   繁体   English

在JavaScript中对时间戳数组进行排序

[英]Sort an array of timestamps in JavaScript

I am developing an Angular web service for creating photo journeys and displaying them on a map using Leaflet. 我正在开发一个Angular Web服务,用于创建照相旅程并将其使用Leaflet显示在地图上。 When I upload the pictures, I get their coordinates from their EXIF data and their DateTimeOriginal from it which returns string like this: 当我上传图片时,我从他们的EXIF数据中获取它们的坐标,并从中获取它们的DateTimeOriginal ,它们返回如下字符串:

在此处输入图片说明

and then save them in array for a further connecting with paths on a map. 然后将它们保存在数组中,以进一步与地图上的路径连接。

I have issues with sorting this array by timestamps in order to connect the markers on the map properly: 为了正确连接地图上的标记,我在按时间戳排序此数组时遇到问题:

在此处输入图片说明

How can I sort by time-stamp? 如何按时间戳排序?

Seems that the dates are invalid. 日期似乎无效。 If it would be properly formatted like this ("2012/10/24 16:37:44") you might do this: 如果将其正确地格式化为这样的格式(“ 2012/10/24 16:37:44”),则可以执行以下操作:

const a = new Date("2012/10/24 16:37:44").getTime();

For example, you would map over items in this array 例如,您可以映射此数组中的项目

function sortNumber(a,b) {
    return a - b;
}

items.map(item => new Date(item.timestamp).getTime()).sort(sortNumber);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM