Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

118
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda