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

119
Views
Array, return original index from a sorted array in javascript

'I wish to sort an array in numerical order but once it is sorted I wish to be able to find the original index.

For example the original array:

ptsGP = [3,8,2,5,6,9,8,4]

I am using the following code below to sort the array:

arr = ptsGP;

var arr2 = arr.map(function(o, i){return {idx: i, obj: o}; }).sort(function(a, b) {
    return b.obj - a.obj;
});

for(var i = 1, j = arr2.length; i <= j; i++){
    document.write('i:' + i + ' = arr2[i].obj:  PTS: ', arr2[i-1].obj+"<br/>");
}`

This is fine as the sorted array is :

arr = [2,3,4,5,6,8,8,9];

How can I find the index of sorted number in the original array? In this case it would be :

Index on original array would be = [2,0,7,3,4,1,6,5]

  • I know I could use map on the original array but how can I deal with duplicate numbers i.e, in this case I have two number 8's within the array?
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can achieve it by following below steps :

  • Creating a deep copy of an original array by using spread operator. So that we can get proper indexing.
  • Now we can iterate deep copy array to get the index of the elements from an original array.
  • Regarding duplicate values we can check via .indexOf() and .lastIndexOf() methods.

via and then via comparison. For fetching the correct index of duplicate values I wrote a logic based on the count of duplicate value.

Working Demo :

// Original array.
const originalArray = [3, 8, 2, 5, 6, 9, 8, 4];

// Creating a deep copy of an original array.
const deepCopy = [...originalArray].sort(function(a, b){
    return a-b
});

// 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.
console.log(arr);

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!