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

141
Views
Array Sorting Number 1-100, pero 100 se muestra como 1er índice?

Tengo un problema cuando ordeno una serie de matrices como esta:

 let number = '72 65 73 78 75 74 90 81 87 65 55 69 72 78 79 91 100 40 67 77 86'; let numberArray = number.split(' '); let sortedNumber = numberArray.sort().map(Number); console.log(sortedNumber);

regresará

 [100, 40, 55, 65, 65, 67, 69, 72, 72, 73, 74, 75, 77, 78, 78, 79, 81, 86, 87, 90, 91]

¿Por qué se muestran 100 en el primer índice? ¿Y cómo hacer que sea el último?

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

0

Prueba esto...

 let number = '72 65 73 78 75 74 90 81 87 65 55 69 72 78 79 91 100 40 67 77 86'; let numberArray = number.split(' '); numberArray.sort(function(a, b) { return a - b; }); console.log(numberArray)

Está tratando de ordenar alfabéticamente... esto se ordenará como un valor numérico

about 4 years ago · Juan Pablo Isaza Report

0

Puede cambiar el algoritmo como:

1) convertir string a string array de cadena

 number.split(" ")

2) mapa sobre la matriz de string para convertirla en una matriz de numbers

 numberArray.map(Number)

3) pass the custom comparator function in the sort method

 .sort((a, b) => a - b)

Nota: Puede hacerlo de una sola línea como

 let sortedNumber = number.split(" ").map(Number).sort((a, b) => a - b); 

 let number = "72 65 73 78 75 74 90 81 87 65 55 69 72 78 79 91 100 40 67 77 86"; let numberArray = number.split(" "); let sortedNumber = numberArray.map(Number).sort((a, b) => a - b); console.log(sortedNumber);
 /* This is not a part of answer. It is just to give the output full height. So IGNORE IT */ .as-console-wrapper { max-height: 100% !important; top: 0; }

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!