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

173
Views
How to sort a deeply sorted array numerically in ascending order?

I am trying to sort each array in an array, however, when I sort each nested array, the arrays are not sorted. This is what I've been doing so far:

let triplets = [ [ 1, -1, 0 ], [ 1, -1, 0 ], [ 1, 0, -1 ] ]

let sortedArrays = triplets.sort((a, b) => a[0] - b[0] || a[1] - b[1])

console.log(sortedArrays) // [ [ 1, -1, 0 ], [ 1, -1, 0 ], [ 1, 0, -1 ] ]

// Expect: [ [-1, 0, 1], [-1, 0, 1], [-1, 0, 1] ]

Obviously, they are not sorted. In the first sub array, 1 does not come before 1. Why is this the case and is there a better way to sort these?

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

0

It looks like you don't need to sort the triplets, you need to sort each subarray - so call .sort on the subarrays, not on the outer array.

let triplets = [ [ 1, -1, 0 ], [ 1, -1, 0 ], [ 1, 0, -1 ] ];
for (const subarr of triplets) {
  subarr.sort((a, b) => a - b);
}
console.log(triplets);

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!