Sort by Length

Published by Matt in

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.

Watch a quick demo on how Edabit works.