← Back to challenges

Sort by Length

JavaScriptHardstringssortingarrayslanguage_fundamentals

Instructions

Create a function that takes a string and returns a string ordered by the length of the words. From shortest to longest word. If there are words with the same amount of letters, order them alphabetically.

Examples

sortByLength("Hello my friend") ➞ "my Hello friend"

sortByLength("Have a wonderful day") ➞ "a day Have wonderful"

sortByLenght("My son loves pineapples") ➞ "My son loves pineapples"

Notes

Punctuation (periods, commas, etc) belongs to the word in front of it.

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.