title: Javascript: Using .sort with compare function to find longest word in array
body: <p>I am a beginner student in Javascript. I have been puzzling over this problem for the last few hours, so I'm hoping that somebody can help me with the the logic in the code I'm trying to write.</p>
  
  <p>I am currently trying to write a function that will take the longest string in an array and pass it back to the console. If there is a tie, the console should return the first word with the most letters.</p>
  
  <p>I found a good piece of code that will do this, but I can't find a way to make it into a function. </p>
  
  <pre><code>arr. sort(function (a, b) { return b.length - a.length })[0];
  </code></pre>
  
  <p>I tried </p>
  
  <pre><code>var longestWord = function(arr) {
      sort(function (a, b) { return b.length - a.length })[0];
  }
  </code></pre>
  
  <p>This doesn't work. I know I'm doing something wrong, but I'm not sure what it is. Also, I can't <em>quite</em> grasp why exactly this words. I can can understand that the function compares the lengths of the two variables, but why does the 0 index have to be at the end?</p>
  
  <p>Any help is greatly appreciated!!</p>