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

103
Views
Javascript sort multiple Arrays by object values inside

Edit**** I believe I messed up the explanation originally. Thank you to Brilliand for pointing that out.

Right now I can map through the data, fine. I want to reorganize it, so I can map and display it by the highest sale of the day to lowest.

data[0].stats.one_day_sales = 2
data[1].stats.one_day_sales = 17
data[2].stats.one_day_sales = 10

The output I am looking for, mind you each [i] contains other data that I want to go along with the reorganization.

data[1].stats.one_day_sales = 17
data[2].stats.one_day_sales = 10
data[0].stats.one_day_sales = 2

I have attached a photo of the data I am receiving as well to better clarify.

Thanks for your help in advance!

Arrays of Objects

about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

Not sure what you are going at, I put your sample data into one before proceeding to sort. What you are looking for is probably the sort function. I sorted them ascending in number but if you want them descending just swap the a with b:

const array1 = [{ stats: { one_day_sales: 7 } }]
const array2 = [{ stats: { one_day_sales: 4 } }]
const array3 = [{ stats: { one_day_sales: 2 } }]

let arrayOfObjects = [array1[0], array2[0], array3[0]]

arrayOfObjects.sort((a, b) => {
    // one day sales being the identifier for sorting
    return a.stats.one_day_sales - b.stats.one_day_sales // swap a with b for descending
})
console.log(arrayOfObjects)

about 4 years ago · Santiago Gelvez Report

0

You could destructure the array and assign the new array in wanted order.

let array1 = [{ stats: { one_day_sales: 7 }}],
    array2 = [{ stats: { one_day_sales: 4 }}],
    array3 = [{ stats: { one_day_sales: 2 }}];
    

[array1, array2, array3] = [array1, array2, array3]
    .sort(([a], [b]) => b.stats.one_day_sales - a.stats.one_day_sales);


console.log(array1);
console.log(array2);
console.log(array3);
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Santiago Gelvez 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!