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

154
Views
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 answers
Answer question

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