'Sorting of array with Moment(date) elements
I have an array that is populated with moment(Date provided by the database) elements. I am trying to sort the array so that the first element is the oldest and the last element is the newest, but with no success.
for (let item of items) {
dates.push(moment(item.created));
}
dates.sort(function(a,b){
var da = new Date(a).getTime();
var db = new Date(b).getTime();
return da < db ? -1 : da > db ? 1 : 0
});
}
console.log(dates);
This always prints the current time times number of elements.
Solution 1:[1]
You can use it like this with moment
dates.sort((a, b) => moment(a.date) - moment(b.date))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Gucal |
