Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

167
Vistas
how to sort array of object by date has specific format

I have array of object contain date format. code works only for ordering the day not for months and years

My Code

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 Respuestas
Responde la pregunta

0

From the above comment ...

"bills.sort((a, b) => new Date(a.date).getTime() - new Date(b.date).getTime()) ... which of cause works for valid date shorthand formats only ... the OP e.g. did provide originally "12 mai 21" whereas it should be "12 may 21" ... it's fixed/edited already."

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 Denunciar

0

You can use this below code to sort your array of object by date has specific format:

    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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda