Sort array of objects with date field by date

后端 未结 5 800
猫巷女王i
猫巷女王i 2021-01-31 03:10

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:         


        
5条回答
  •  长情又很酷
    2021-01-31 03:50

    If you are trying to use lodash to sort dates in ascending or descending order for your array of objects, you should use _.orderBy instead of _.sortBy

    https://lodash.com/docs/4.17.15#orderBy, this method allows specifying sort orders either by 'asc' or 'desc'

    An example would be:

    const sortedArray = _(myArray.orderBy([
          function(object) {
            return new Date(object.date);
          }],["desc"])
    

提交回复
热议问题