Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to sort the string according to the integer values

Here i have tried

let sen = ("is2 Thi1s T4est 3a")

let sen2 = sen.split(" ")

//console.log(sen2)

sen2.sort()

console.log(sen2)

the output should be like this

rearrange("is2 Thi1s T4est 3a") ➞ "This is a Test"

rearrange("4of Fo1r pe6ople g3ood th5e the2") ➞ "For the good of the people"

rearrange(" ") ➞ ""

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can try something like this

function rearrange(sen){
let sen2 = sen.split(" ");
sen2.sort(function(a, b){
   return parseInt(a.replace(/\D/g,'')) - parseInt(b.replace(/\D/g,''));
});
sen2 = sen2.map(a => a.replace(/\d/g,''));
return sen2.join(" ");
}

You may also need some error handling (In case some words dont have numbers)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading