Lo-Dash sortBy array of dates in string format

前端 未结 2 2356
说谎
说谎 2021-02-20 00:07

I\'d like to know why lodash doesn\'t sort array of dates in string format as compared with plain javascript sort(). Is it expected behavior or a bug?



        
2条回答
  •  名媛妹妹
    2021-02-20 00:32

    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"])
    

提交回复
热议问题