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

175
Views
How to sort the date form to latest to the oldest in array of objects react js

I'm trying to sort the date from this array of objects from the latest to the oldest. I'm trying this code, i.e. copy the array to a new const and then use it with sort, but I have the same result, no sorted date. My code is below:

const array=[...user?.editedBy]
console.log(array,"array")

const sortedDates = array?.sort((dateA, dateB) => dateB.date -dateA.date )
console.log(sortedDates,'sortedArray')

The first pic is the console log from the array and the second from sortedDates.

So the same result, can someone please help me figure out what I'm missing?

Thank you in advance

enter image description here enter image description here

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

0

As you can see in your console screenshot, those aren't actually Date objects, but strings.

So you have to map the strings to Date objects and compare them afterward.

const sortedDates = array?
  .map(obj => { return { ...obj, date: new Date(obj.date) } })
  .sort((a, b) => b.date - a.date)
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!