I need to order an array of dates with their times in descending order but the times remain in ascending order with lodash.
const supplierDates = ["2022-02-14 07:00", "2022-02-14 08:00", "2022-02-14 10:00", "2022-02-17 19:00", "2022-02-18 18:00"
results = _.orderBy(supplierDates, suppDate => suppDate, ['desc']);
This sorts the array in descending order but I am interested in maintaining an ascending order for SAME day times (in this case, 14th Feb)
>Solution :
This happens because you are sorting strings, while you want to sort dates.
Use substring to extract ONLY the date from your strings, and then use Date.parse to parse to dates so you can sort them:
const supplierDates = ["2022-02-14 07:00", "2022-02-14 08:00", "2022-02-14 10:00", "2022-02-17 19:00", "2022-02-18 18:00"];
const results = _.orderBy(supplierDates, suppDate => Date.parse(suppDate.substring(0, 10)), ['desc']);
console.log(results);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js"></script>