I have an array of such objects:
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 time value.
I 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 also referred to the Mozilla Array.prototype.sort () documentation and tried the following that 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; }
source share