# Different Longest Word in a String Solution

Different Longest Word in a String Solution
0

#1

I got a solution that works but I want to know how it stacks up to the solutions that are shown.
This is what I did.

``````function findLongestWord(str) {
var longToShort = str.split(' ').sort(function(a, b) {
return b.length - a.length;
});

return longToShort[0].length;
}

findLongestWord("The quick brown fox jumped over the lazy dog");
``````

unlisted #2

#4

oh! I’m sorry. That wasn’t my intention. I guess I wasn’t clear on what I was looking for. I was hoping someone could give some understanding on why this isn’t a suggested way to solve this and what issues I could encounter from doing this way.

#5

There are many ways to solve this challenge. If you look at the Hint section, you will see your solution (splitting, sorting, taking length of first element) is very similar to the other solutions.

#6

Is it more work for the program to have to sort the entire array, especially if it is very large, rather than starting on the first element and checking one at a time like in the “advance solution”?

#7

The sort version (like yours) has a time complexity of O(nLogn) vs O(n) of the Basic Solution. The Advanced Solution is actually slower than the Basic, because of the splice function. The splice function has to readjust/reorder the array after an item is removed.

listed #8