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

122
Views
Sorting Js Array according to pre-defined array of indexes

hope you can help me fix this.

So I have one array with nested arrays:

mainArr: [[Carl, 20, Male],[Mike, 30, Male], [Chloe, 45, Female], ...]

Now this array needs to be sorted along another array containing a set order of indexes from the array above.

indexArr: [2,1,3,...]

Result here being:

sortedArr: [[Mike, 30, Male], [Carl, 20, Male], [Chloe, 45, Female], ...]

I thought about writing a ForEach loop with the second array, something along the lines of:

indexArr.forEach(i){
 sortedArr.push(mainArr[indexArr[i]]);
};

But I'm trying to avoid using loops with this function to keep it fast. I couldn't find anything relevant to my problem in any array documentation. Do you know a way to fill sortedArr or sort mainArr efficiently?

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

0

    indexArr.map(ind => mainArr[ind - 1])
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!