I have a number of objects in an array. The objects have a 'time' property which is a date string.
items = [
{time: "2013-03-01T10:46:11Z"},
{time: "2013-03-03T10:46:11Z"},
{time: "2013-03-02T10:46:11Z"}
]
I wish to sort the array by that 'time' property.
I've read Sort Javascript Object Array By Date and Javascript Date Sorting, but I can't seem to make either of these solutions (either converting to Date objets or sorting as strings) work.
My sort function:
items.sort(function(first, second){
return new Date(first.time) < new Date(second.time) ? 1 : -1;
})
Testing the results:
items.forEach(function(item){
console.log(item.time)
})
Returns:
2013-03-01T10:46:11Z
2013-03-03T10:46:11Z
2013-03-02T10:46:11Z
March 1, March 3, March 2. What am I doing wrong?