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. 回答1: