title: how to sort array which contains numbers?
body: <p>I am trying to sort an array. </p>
  
  <p>Ex-</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>let arr = [{label: "Name 5"}, {label: "Name 3"},{label: "Name 12"}, {label: "Name 10"}, {label: "First Name 5"}, {label: "Apple"}, {label: "Orange"}, {label: "water"}];
  
  
  let sortedArray = arr.sort(function(a, b){
   return a.label.localeCompare(b.label);
  });
  
  console.log(sortedArray);</code></pre>
  </div>
  </div>
  </p>
  
  <p>When I try to sort it, "Name 10" comes first but "Name 3" should come fist.</p>
  
  <p>I have also tried this -</p>
  
  <pre><code>let sortedArray = arr.sort(function(a, b){
    var nameA=a.label.toLowerCase(), nameB=b.label.toLowerCase();
      if (nameA &lt; nameB){
        return -1;
      } //sort string ascending
      if (nameA &gt; nameB){
        return 1;
      }
     return 0; //no sorting
  });
  </code></pre>
  
  <p>And this -</p>
  
  <pre><code>Array.prototype.reverse()
  String.prototype.localeCompare()
  </code></pre>
  
  <blockquote>
    <p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort" rel="nofollow noreferrer">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort</a></p>
  </blockquote>
  
  <p>But still no luck. Can anyone point out whats wrong here?</p>