Give the following array of objects, I need to sort them by the date field ascending.
var myArray = [
{
name: \"Joe Blow\",
date: \"Mon Oct 31 2016 00:
Your date values are strings, so you need to use the new Date()
constructor to change them to javascript date
objects. This way you can sort them (using _.sortBy
).
var myArray = [
{
name: "Joe Blow",
date: "Mon Oct 31 2016 00:00:00 GMT-0700 (PDT)"
},
{
name: "Sam Snead",
date: "Sun Oct 30 2016 00:00:00 GMT-0700 (PDT)"
},
{
name: "John Smith",
date: "Sat Oct 29 2016 00:00:00 GMT-0700 (PDT)"
}
];
myArray = _.sortBy(myArray, function(dateObj) {
return new Date(dateObj.date);
});
console.log(myArray)