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

284
Vistas
Javscript(Vue) Sort Object with arrays by date

I have an Object Structure like this:

{
  1000: [{AnnouncementCode: 1000, Name: 'foo', created_at: "2022-02-01T19:52:01.000000Z"},{AnnouncementCode: 1000, Name: 'foo', created_at: "2022-02-01T19:52:01.000000Z"},{AnnouncementCode: 1000, Name: 'foo', created_at: "2022-02-01T19:52:01.000000Z"}],
  2000: [{AnnouncementCode: 2000, Name: 'foo', created_at: "2022-02-01T19:52:01.000000Z"},{AnnouncementCode: 2000, Name: 'foo', created_at: "2022-02-01T19:52:01.000000Z"},{AnnouncementCode: 2000, Name: 'foo', created_at: "2022-02-01T19:52:01.000000Z"}],
  3000: [{AnnouncementCode: 3000, Name: 'foo', created_at: "2022-02-01T19:52:01.000000Z"},{AnnouncementCode: 3000, Name: 'foo', created_at: "2022-02-01T19:52:01.000000Z"},{AnnouncementCode: 3000, Name: 'foo', created_at: "2022-02-01T19:52:01.000000Z"}],
  4000: [{AnnouncementCode: 4000, Name: 'foo', created_at: "2022-02-01T19:52:01.000000Z"},{AnnouncementCode: 4000, Name: 'foo', created_at: "2022-02-01T19:52:01.000000Z"},{AnnouncementCode: 4000, Name: 'foo', created_at: "2022-02-01T19:52:01.000000Z"}]
}

I am printing out all in the frontend but for now they are sorted inside every array but not overall. I don't know what would be the best way to sort all values from every key globally.

The Sorting should be by date, so by 'created_at'

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

0

I think is not worth it to sort a data like this, because will be more useful for map and all just have an array of objects, like this:

const array = [
  {AnnouncementCode: 4000, Name: 'foo', created_at: "2022-04-01T19:52:01.000000Z"},
  {AnnouncementCode: 4000, Name: 'foo', created_at: "2022-07-01T19:52:01.000000Z"},
  {AnnouncementCode: 3000, Name: 'foo', created_at: "2022-05-01T19:52:01.000000Z"},
  {AnnouncementCode: 1000, Name: 'foo', created_at: "2022-09-01T19:52:01.000000Z"},
  {AnnouncementCode: 2000, Name: 'foo', created_at: "2022-01-01T19:52:01.000000Z"},
  {AnnouncementCode: 1000, Name: 'foo', created_at: "2022-02-01T19:52:01.000000Z"},
]

const sortedByDate = array.sort((a,b) => {
  if (a.created_at > b.created_at) return 1
  if (a.created_at < b.created_at) return -1
  if (a.created_at == b.created_at) return 0
})

const sortByAnnouncementCode = array.sort((a,b) => {
  if (a.AnnouncementCode > b.AnnouncementCode) return 1
  if (a.AnnouncementCode < b.AnnouncementCode) return -1
  if (a.AnnouncementCode == b.AnnouncementCode) return 0
})

const sortByCodeThanDate = 
  array
  .filter((data) => data.AnnouncementCode === 1000)
  .sort((a,b) => {
    if (a.created_at > b.created_at) return 1
    if (a.created_at < b.created_at) return -1
    if (a.created_at == b.created_at) return 0
  })

console.log(sortByCodeThanDate)

If you really need to sort by the data you informed I think you will have a tougher task but all depends if area dynamics data or no.

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