title: Sort array on key value
body: <p>I have a function which sorts by name currently and an array of value / key pairs.</p>
  
  <p>I wonder how can I pass the key on which sort is being performed so I can call the same function every time like so:</p>
  
  <pre><code>var arr = [{name:'bob', artist:'rudy'},
             {name:'johhny', artist:'drusko'},
             {name:'tiff', artist:'needell'},
             {name:'top', artist:'gear'}];
  
  sort(arr, 'name');   //trying to sort by name
  sort(arr, 'artist'); //trying to sort by artist
  
  function sort(arr) {
    arr.sort(function(a, b) {
      var nameA=a.name.toLowerCase(), nameB=b.name.toLowerCase();
      if (nameA &lt; nameB) //sort string ascending
        return -1;
      if (nameA &gt; nameB)
        return 1;
      return 0; //default return value (no sorting)
     });          
  }
  </code></pre>