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

233
Views
Javascript, ordene una matriz de números de mayor a menor pero mantenga el índice original

Tengo una matriz de números que quiero ordenar de mayor a menor y aún así poder mantener la matriz original para hacer referencia a los índices originales.

Me las he arreglado para lograr lo que quiero con el código aquí:

 // 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 ba }); // 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");

El problema que tengo es donde hay números duplicados; en este caso, el número ocho aparece tres veces, pero cuando se ordena, solo devuelve el índice de la primera incidencia del número, en lugar de mostrar los índices 1,3,6.

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

0

Para elaborar el comentario de @epascarello, puede mapear la matriz copiada para incluir el índice de cada elemento antes de ordenar. no es necesario separar los índices en una matriz separada.

 // 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!