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

152
Vistas
Sort an array of object with key in JavaScript

I am trying to sort the array of an objects with key which is date which looks like this

myArray = [{
  '2022-04-01': '0.01'
}, {
  '2022-03-31': '0.02'
}, {
  '2022-04-02': '0.05'
}]

But I want to have like this

myArray = [{
  '2022-03-31': '0.02'
}, {
  '2022-04-01': '0.01'
}, {
  '2022-04-02': '0.05'
}],

I tried diff num of sols, which already there on stackoverflow, but none of them match to my case.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Just this will work.

myArray.sort((a,b) => Object.entries(a)[0] < Object.entries(b)[0] ? -1 : 1);

Running example below:

const myArray = [
  { '2022-03-31': '32.2'   }, 
  { '2022-04-01': '32.23'  }, 
  { '2022-04-02': '32.9'   }, 
  { '2022-03-30': '32.253' }, 
  { '2022-04-03': '32.253' },
  { '2022-03-18': '32.253' },
  { '2021-06-31': '32.37' }
];

myArray.sort((a,b) => Object.entries(a)[0] < Object.entries(b)[0] ? -1 : 1);

console.log(myArray);

You can also use Object.keys instead of Object.entries like:

Object.keys(a)[0]
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