Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

208
Views
How to orderBy an array of dates(desc) while maintaining (asc) SAME day times

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 10:00", "2022-02-14 08:00", "2022-02-14 09: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)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This happens because you are sorting strings, while you want to sort dates.

First, order the whole dates by asc order, to maintain hours for each day.

Then 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 08:00", "2022-02-14 10:00", "2022-02-14 07:00", "2022-02-17 19:00", "2022-02-18 18:00"];
const initialOrder = _.orderBy(supplierDates, suppDate => Date.parse(suppDate), ['asc']);
const results = _.orderBy(initialOrder, 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>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!