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

150
Vistas
Sort a string in Javascript

I have a string like the following. The string contains words which contain a number from 0 to 9 in them The number is the position they must have in the resulting string. If the string is empty, the result will be an empty string

Example: "en5 4terremoto NAS1A regi2stró L0a M6arte u3n"

Resultado:

 "L0a NAS1A regi2stró u3n 4terremoto en5 M6arte"

how to go through the chain and order it, taking into account the number of each word?

Until now, I applied the split method to the original chain to separate them.

const mensaje = 'en5 4terremoto NAS1A regi2stró L0a M6arte u3n';

const arr = mensaje.split(' ');
console.log(arr);

result

I appreciate your help

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You could take each word, and remove all non-digits from it (with a regular expression), and use that as index in a result array:

const mensaje = "en5 4terremoto NAS1A regi2stró L0a M6arte u3n";

const result = [];
for (const word of mensaje.split(" ")) {
    result[word.replace(/\D/g, "")] = word;
}
console.log(result);

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