Given the following array of objects, I need to ascending sort them by the date field.
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)" } ]; In the above example, the final result would be John Smith, Sam Snead, and Joe Blow.
I am trying to use lodash's _.sortBy(), but I can't get any sorting to take place 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 get my array sorted properly? I also have Moment.js, so I can use it to format the date string if needed. I tried converting the date property using .unix(), but that didn't make a difference.
Thanks.
_.sortBydoesn't seem to sort in place (unlike Array#sort method)_.sortBy(myArray, ['date'])should work. For'2018-08-28T17:38:00'date format it works.