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

113
Views
How to sort through an array primarily with first elements and then if they are same, then with second elements. So x-y axis in ascending order

How do I sort array so that this output

[ [ '6.9', '14.5' ], [ '22.2', '28.9' ] ]

is sorted primarily with x value so 6.9 and 22.5 in the above example and secondarily with y axis, 14.5 and 28.9 in the above example.

this is my code currently to get the output.

const mySet = uniqueArray4(x_y_total);

    mySet.forEach(element => {
        let sumx = 0, sumy = 0;
            for(let i = 0; i < element.length; i++) {
                sumx += element[i][0]/element.length;
                sumy += element[i][1]/element.length;
            }
            
            answer.push([sumy.toFixed(1), sumx.toFixed(1)]);
            });
        
    answer.forEach(element => {
        finalanswer.push(element);
    })

Quite new to js, I searched through google but it's been quite difficult as to how should I implement this.

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

0

You can sort based on the 1st entry, and then on the 2nd entry, something like:

// Sorting on 1st param
answer.sort((a,b) => a[0] - b[0]);

// Sorting on 2nd param given then 1st param is sorted, if not, return 0
answer.sort((a,b) => a[0] < b[0] ? a[1] - b[1] : 0);
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!