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

164
Views
cómo ordenar una matriz de objetos por fecha tiene un formato específico

Tengo una matriz de objetos que contienen formato de fecha. el código funciona solo para ordenar el día no por meses y años

Mi código

 const bills = [ { name: "ghaith", type: "transport", date: "12 may 21", }, { name: "Alex", type: "Restaurants", date: "15 oct 20", } ]; bills.sort((a, b) => b.date < a.date ? 1 : -1)

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

0

Del comentario anterior...

" bills.sort((a, b) => new Date(a.date).getTime() - new Date(b.date).getTime()) ... cuya causa funciona solo para formatos abreviados de fecha válidos. ... el OP, por ejemplo, proporcionó originalmente "12 mai 21" , mientras que debería ser "12 may 21" ... ya está arreglado/editado".

 const bills = [{ name: "ghaith", type: "transport", date: "12 may 21", }, { name: "Alex", type: "Restaurants", date: "15 oct 20", }]; console.log( bills.sort((a, b) => // new Date(a.date).getTime() - new Date(b.date).getTime() // or directly without `getTime` ... new Date(a.date) - new Date(b.date) ) );
 .as-console-wrapper { min-height: 100%!important; top: 0; }

about 4 years ago · Juan Pablo Isaza Report

0

Puede usar este código a continuación para ordenar su matriz de objetos por fecha tiene un formato específico:

 const bills = [{ name: "ghaith", type: "transport", date: "12 may 21", }, { name: "Alex", type: "Restaurants", date: "15 oct 20", }]; console.log( bills.sort((a, b) => new Date(a.date) - new Date(b.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!