DEV Community

loading...
Cover image for Day 6 of JavaScriptmas - Sort By Length Solution

Day 6 of JavaScriptmas - Sort By Length Solution

Sekti
Keep moving forward. Learn well not learn fast.
・1 min read

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

Discussion (0)