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

220
Views
Javascript, Sort an array of numbers from highest to lowest but keep original index

I have an array of numbers that I want to sort from highest to lowest and still be able to keep the original array to reference the original indexes.

I have managed to accomplish what I want with the code here:

// Original array.
const originalArray = [3, 8, 2, 8, 6, 9, 8, 4];
// Creating a deep copy of an original array.
const deepCopy = [...originalArray].sort(function(a, b){
    return b-a
});
// result array
const arr = [];
// count to get the index based on duplicate values.
let count = 0;

// Iterating deepCopy array to get the actual index.
deepCopy.forEach((elem) => {
    // Checking for duplicate value in an array
    if(originalArray.indexOf(elem) === originalArray.lastIndexOf(elem)) {
        // This line of code execute if there is no duplicates in an array.
        arr.push(originalArray.indexOf(elem))
    }else{
        // This line of code execute if there is duplicate values in an array. 
        count++;
        // Inserting the index one by one.
        arr.push(originalArray.indexOf(elem, count))
    }
});

// Result array.
document.write("Original Array<br/>"+originalArray+"    : Original Array");
document.write("<br/><br/>");
document.write("Original Index<br/>"+arr+"  : Orignal Index");
document.write("<br/><br/>");
document.write("Deep Copy<br/>"+deepCopy+"  : Sorted Array");
document.write("<br/><br/>");
// Index[0] = Original Array [2];   
for(n=0; n<arr.length; n++){
    a2 = originalArray.indexOf(deepCopy[n]);
    document.write(n+"] "+originalArray[a2]+" - Original Index: "+a2+"<br/>");
};

document.write("<br/>For the Number 8 at position 1,2,3- The indexes shold read 1,3 & 6 instead of three consecutive 1's");

The problem I have is where there are duplicate numbers - in this case the number eight appears three times but when sorted it only return the index of the first incidence of the number - instead of displaying the indexes 1,3,6.

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

0

To elaborate @epascarello comment, you can map the copied array to include the index for each element before sorting. no need to separate indexes into a separate array.

// Original array.
const originalArray = [3, 8, 2, 8, 6, 9, 8, 4];
// Creating a deep copy of an original array.
const deepCopy = [...originalArray].map((e, i) => [e, i]).sort(function(a, b){
    return b[0]-a[0]
});

// Result array.
document.write("Original Array<br/>"+originalArray+"    : Original Array");
document.write("<br/><br/>");
document.write("Deep Copy<br/>"+deepCopy+"  : Sorted Array");
document.write("<br/><br/>");
 
let n = 0
for(let [e, i] of deepCopy){
    n++
    document.write(n+"] "+ e +" - Original Index: "+ i +"<br/>");
};

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!