← Back to challenges

Sort an Array by String Length

JavaScriptHardarrayssortingalgorithms

Instructions

Create a function that takes an array of strings and return an array, sorted from shortest to longest.

Examples

sortByLength(["Google", "Apple", "Microsoft"])
➞ ["Apple", "Google", "Microsoft"]

sortByLength(["Leonardo", "Michelangelo", "Raphael", "Donatello"])
➞ ["Raphael", "Leonardo", "Donatello", "Michelangelo"]

sortByLength(["Turing", "Einstein", "Jung"])
➞ ["Jung", "Turing", "Einstein"]

Notes

All test cases contain arrays with strings of different lengths, so you won't have to deal with multiple strings of the same length.

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.
Next: Check If It's a Title String