DEV Community

Cover image for Day 6 of JavaScriptmas - Sort By Length Solution
Sekti Wicaksono
Sekti Wicaksono

Posted on

Day 6 of JavaScriptmas - Sort By Length Solution

Day 6 is how to sort an array that has different length of value.
This is helpful to sort name based on length of the name in the array by using sort method.

For instance an array ['Samuel','Jon','Karra','Hery'] sorted into ['Jon','Hery','Karra','Samuel']

This is JavaScript solution

function sortByLength(strs) {
    return strs.sort((a,b) => a.length - b.length);
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)