title: Javascript sort object by integer field
body: <p>I have an array of objects on which one of the fields is an integer value and i would like to sort using its field.</p>
  
  <p>For example:</p>
  
  <pre><code>var myArray = {
      { name: "Robert", age: 47 },
      { name: "Carl", age: 22 },
      { name: "Ronaldo", age: 15 }
  };
  
  myArray.sort(function(a, b){ a.age - b.age });
  </code></pre>
  
  <p>The actual program run 24h and sort array every minute, sometimes it sorts correctly (no code changes), and sometimes (most of them) it does what appears to be random sort.</p>
  
  <p>The desired output would be:</p>
  
  <pre><code>myArray = {
  { name: "Ronaldo", age: 15 },
  { name: "Carl", age: 22 },
  { name: "Robert", age: 47 }
  }
  </code></pre>
  
  <p>but each time I run the code, it gives different orders as it wasn't sorting at all.</p>
  
  <p>The real array has like 0-3000 entries and each object has a lot of properties. I don't know if it related to the size of the array or the data struct.</p>
  
  <p>Already searched everywhere and everybody tells to pass a custom JS function to sort the array. This function would compare then with "ifs" or subtract them but I can't find anyone talking about this... </p>
  
  <p>I tried wrap <code>a.age</code> and <code>b.age</code> into <code>parseInt()</code> 's and</p>
  
  <pre><code>myArray = myArray.sort(function(a, b){a.age - b.age});
  </code></pre>