Your task is to sort a given string.
Each word in the string contains a separate number, which represents the position of the word in the string. The number
is between1and9, so the word containing1will be the first word.
If the given string is empty, return an empty string.
For example:"is2 Thi1s T4est 3a"
Return:"Thi1s is2 3a T4est"
Okay, what? Let’s see how to write such a function.
1. First split the given string according to spaces. After all, arrays are easier to operate than strings.
2. Then formulate sorting rules. Which word contains a larger number will be ranked lower.
3. Then, use thesortmethod of the array to pass in the sorting rule anonymous function to perform customized sorting.
4. Finally, aggregate the array aftersortand return a string.
This question is quite easy, and skilled students can solve it quickly.
function findNumber(str){ for(var i=0;i Copy after login
The above is the content of JavaScript interesting questions: string sorting. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!