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

238
Views
Cómo ordenar la cadena de formato de fecha en una matriz

Aporte

 var data = ["09 may 2015", "25 december 2015", "22 march 2015", "25 june 2016", "18 august 2015"];

salida 22 de marzo de 2015, 09 de mayo de 2015, 18 de agosto de 2015, 25 de diciembre de 2015, 25 de junio de 2016

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

0

Restar 2 fechas devuelve la diferencia entre las dos fechas en milisegundos si a fecha es más pequeña que b devuelve valores negativos a se ordenará para ser un índice más bajo que b .

 var data = ["09 may 2015", "25 december 2015", "22 march 2015", "25 june 2016", "18 august 2015"]; data.sort(function(a,b){ return new Date(a) - new Date(b); }); console.log(data);

about 4 years ago · Juan Pablo Isaza Report

0

Como se señaló en los comentarios, una solución simple sería usar new Date() . Dado que OP solicitó un posible método sin new Date() , la siguiente solución puede lograr el objetivo deseado.

Fragmento de código

 // set-up a map/object to map "january": "01", "february": "02", etc const monthMap = Object.fromEntries( "january,february,march,april,may,june,july,august,september,october,november,december" .split(',').map( (month, idx) => ([month, idx.toString().padStart(2, '0')]) ) ); // use the map/object to convert month into 2-char string const monthToNum = m => m.toLowerCase() in monthMap ? monthMap[m.toLowerCase()] : '99'; // reformat date from dd MMM yyyy to yyyy-mm-dd const reformatDate = dt => { const parts = dt.split(' '); return [parts[2], monthToNum(parts[1]), parts[0]].join('-'); }; // use the custom-made "reformat" method to sort const sortData = arr => arr.sort((a, b) => reformatDate(a) > reformatDate(b) ? 1 : -1); // actual data from OP's question const data = ["09 may 2015", "25 december 2015", "22 march 2015", "25 june 2016", "18 august 2015"]; // invoke the sort-method and console.log the result console.log(sortData(data));

Explicación

Los comentarios se han agregado en línea dentro del fragmento de código anterior. Si tiene más preguntas, utilice los comentarios a continuación.

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!