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

207
Views
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(" ") ➞ ""
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can split based on the numeric value by selecting the number present in word by regex and sort on this numeric value.

const rearrange = (sentence) => {
  if(sentence.trim() === "") return "";
  return sentence
    .trim()
    .split(" ")
    .map(word => [word.match(/\d+/)[0], word])
    .sort((a, b) => a[0] - b[0])
    .map(([, word]) => word.replace(/\d+/, ''))
    .join(" ");
}

console.log(rearrange("is2 Thi1s T4est 3a")) //"This is a Test"
console.log(rearrange("4of Fo1r pe6ople g3ood th5e the2"))  //"For the good of the people"
console.log(rearrange(" ")) // ""

about 4 years ago · Juan Pablo Isaza Report

0

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)

about 4 years ago · Juan Pablo Isaza Report

0

function getNumber(s) {
    var r = /\d+/;
    return s.match(r) || 0;
}

function reArrange(s) {
    let s2 = s.split(" ")
    s2.sort(function(a, b){return getNumber(a) - getNumber(b)});
    return s2.join(' ')
}

console.log(reArrange("4of Fo1r pe6ople g3ood th5e the2"))
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!