I have an array of objects like so :
var example = [{
"description": "aaa",
"time": "12:15pm"
}, {
"description": "bbb",
"time": "10:10am"
}, {
"description": "ccc",
"time": "4:00pm"
}, {
"description": "ddd",
"time": "6:15pm"
}, {
"description": "eee",
"time": "1:10am"
}, {
"description": "fff",
"time": "5:00pm"
} ];
I want to sort by the time value.
I have tried to apply this solution which was intended for an array of string values:
example.sort(function (a, b) {
return new Date('1970/01/01 ' + a.time) - new Date('1970/01/01 ' + b.time);
});
console.log(example);
I've also been referring to the Mozilla Array.prototype.sort() documentation and tried the following which didn't seem to work:
example.sort(function(a, b) {
if (new Date(a.time) > new Date(b.time)) {
return 1;
}
if (new Date(a.time) < new Date(b.time)) {
return -1;
}
// a must be equal to b
return 0;
});
console.log(example);
new Date("1:10am"); //Invalid Date