Sort Complex JavaScript Array
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/979256/how-to-sort-an-array-of-javascript-objects">How to sort an array of javascript objects?</a>  </p>
</blockquote>



<p>I'm trying to convert some of my old ActionScript 2.0 code to JavaScript. Everything works fine but I am unable to sort my array numerically by score.</p>

<p>In my original ActionScript code I used:</p>

<pre><code>// Sort the capitals array so that the capitals with the lowest score will be at the top
capitalsList.sortOn("score", Array.NUMERIC);
</code></pre>

<p>This worked fine but does not work in JavaScript. I changed it to:</p>

<pre><code>capitalsList.sort("score", Array.NUMERIC);
</code></pre>

<p>But Alabama is still outputted (Connecticut should be outputted since its score is -1). What am I missing? </p>

<p>My script on JS Bin: <a href="http://jsbin.com/ibitez/1/edit" rel="nofollow">http://jsbin.com/ibitez/1/edit</a></p>

<pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;script&gt;
window.onload = function() {

    function capitalsConstructor(capitalName, stateName, score) {
        this.capitalName = capitalName;
        this.stateName = stateName;
        this.score = score;
    }

    // Create state capitals array
    var capitalsList = new Array();

    // All 50 capitals are loaded into the array with a score of 0
    // Sample limited to 10 capitals
    capitalsList[0] = new capitalsConstructor("Montgomery", "Alabama", 0);
    capitalsList[1] = new capitalsConstructor("Juneau", "Alaska", 0);
    capitalsList[2] = new capitalsConstructor("Phoenix", "Arizona", 0);
    capitalsList[3] = new capitalsConstructor("Little Rock", "Arkansas", 0);
    capitalsList[4] = new capitalsConstructor("Sacramento", "California", 0);
    capitalsList[5] = new capitalsConstructor("Denver", "Colorado", 0);
    capitalsList[6] = new capitalsConstructor("Hartford", "Connecticut", -1);
    capitalsList[7] = new capitalsConstructor("Dover", "Delaware", 0);
    capitalsList[8] = new capitalsConstructor("Tallahassee", "Florida", 0);
    capitalsList[9] = new capitalsConstructor("Atlanta", "Georgia", 0);

    capitalsList.sort("score", Array.NUMERIC);

    solution = capitalsList[0].stateName;

    document.getElementById("divSolution").innerText = solution;
}
&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;div id="divSolution"&gt;&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
