Give the following array of objects, I need to sort them by date field in ascending order.
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)"
}
];
so in this example, the end result will be John Smith, Sam Sned, and Joe Bloov.
I am trying to use lodash _. sortBy () , but I can not go through sorting, no matter how I try to use it:
_.sortBy(myArray, function(dateObj) {
return dateObj.date;
});
or
_.sortBy(myArray, 'date');
What do I need to change to arrange my array? I also have Moment.js, so I can use it to format the date string if necessary. I tried converting the date property with .unix (), but that did not help.
Thank.
source
share