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

114
Views
Map and sort data by date

I retrieve an array of objects and I need to format it to make it acceptable for Google Charts. The initial data looks like so

[
   {
      "itemOne":111,
      "itemTwo":1,
      "itemThree":"2022-02-01T00:00:00.000Z"
   },
   {
      "itemOne":222,
      "itemTwo":2,
      "itemThree":"2022-01-01T00:00:00.000Z"
   },
   {
      "itemOne":333,
      "itemTwo":3,
      "itemThree":"2021-12-01T00:00:00.000Z"
   },
]

To start the formatting, I do the following

const chartData = data.lastYear.map(
    (item) => ([item.itemThree, item.itemTwo, item.itemOne]),
);

Which now leaves me with this.

[
    ["2022-02-01T00:00:00.000Z", 1, 111],
    ["2022-01-01T00:00:00.000Z", 2, 222],
    ["2021-12-01T00:00:00.000Z", 3, 333],
]

I have been trying however to sort the above by date. My current attempt is pretty bad, and obviously doesnt work.

 const chartData = data.lastYear.map(
    (item) => ([item.itemThree, item.itemTwo, item.itemOne]),
  ).sort((a, b) => {
    return new Date(b.date) - new Date(a.date);
  });
  

So what would be the best way to sort everything by date?

Thanks

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

0

let data = [
   {
      "itemOne":111,
      "itemTwo":1,
      "itemThree":"2022-02-01T00:00:00.000Z"
   },
   {
      "itemOne":222,
      "itemTwo":2,
      "itemThree":"2022-01-01T00:00:00.000Z"
   },
   {
      "itemOne":333,
      "itemTwo":3,
      "itemThree":"2021-12-01T00:00:00.000Z"
   },
]
data.sort((a,b)=>{
    return new Date(b.itemThree) - new Date(a.itemThree);
})

This sorts your data array according to date. Hope this is helpful

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!