1

I am developing an Angular web service for creating photo journeys and displaying them on a map using Leaflet. When I upload the pictures, I get their coordinates from their EXIF data and their DateTimeOriginal from it which returns string like this:

enter image description here

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:

enter image description here

How can I sort by time-stamp?

Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
Deyan Peychev
  • 77
  • 3
  • 7
  • 2
    Possible duplicate of [Sort a string date array](https://stackoverflow.com/questions/30691066/sort-a-string-date-array) – bugs May 22 '18 at 12:20

1 Answers1

4

Seems that the dates are invalid. If it would be properly formatted like this ("2012/10/24 16:37:44") you might do this:

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);
Javid Asgarov
  • 1,511
  • 1
  • 18
  • 32
  • 1
    What I did is to replace the **:** in the time-stamp string and make it appropriate for parsing to a Date object, and then sorting the array by dates: `sortMarkers() { this.currentMarkers.map(m => { let mTimestamp: string = m.timestamp.substring(0, 10); mTimestamp = mTimestamp.replace(/:/g, '-'); m.timestamp = mTimestamp + m.timestamp.substring(10); m.date = new Date(m.timestamp); }); this.currentMarkers = this.currentMarkers.sort(function (a, b) { return a.date - b.date; }); }` – Deyan Peychev May 27 '18 at 11:35