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

283
Views
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 answers
Answer question

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 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!