Sort table by selected value
<p>I have an online rugby game and I want to make a page where users have displayed in a table with only one column player name. If they select something from the dropdown list (speed, kicking, etc), the players are sorted based on what user selected.
Here is what I tried to do:</p>

<pre><code>&lt;script type="text/javascript"&gt;
var arr_head = ['name','age','ss','experience','leadership','weight','height','stamina','strength','breakthroughs','tackling,'handling','speed','kicking'];
var arr_ord = ['asc','asc','desc','desc','desc','desc','desc','desc','desc','desc','desc','desc','desc','desc'];
var arr_players = new Array();
var col = 0;
// the array bellow is generated with a function: $arr_players = player_list($teamid);
arr_players[0] = ['Bruce Ewing',20,151,2,3,84,182,21,22,21,23,24,23,17];
arr_players[1] = ['Barbu Frates',18,113,2,3,105,185,3,99,3,1,2,3,2];
arr_players[2] = ['Costi Buhaescu',24,116,2,1,95,198,2,99,2,3,4,4,2];
arr_players[3] = ['Corneliu Kirita',20,113,1,1,88,183,1,99,2,4,2,4,1];
arr_players[4] = ['Cristi Stelea',23,116,2,4,93,197,2,99,2,4,4,4,1];
arr_players[5] = ['Codrin Sarapatin',23,115,2,5,103,200,3,99,2,4,1,3,3];
arr_players[6] = ['Cosmin Tadici',19,113,1,5,99,203,2,99,2,1,2,4,3];
arr_players[7] = ['Dudu Dumea',21,115,1,5,87,179,2,99,3,3,2,4,2];
arr_players[8] = ['Dorian Hizo',24,115,1,4,98,195,4,99,3,2,1,3,3];
arr_players[9] = ['Ernest Cazacu',18,113,2,5,93,193,2,99,3,1,1,3,4];
arr_players[10] = ['Emil Siman',18,115,1,2,88,187,4,99,2,3,2,1,4];
arr_players[11] = ['Grigorie Banciu',20,116,2,5,102,198,3,99,2,4,2,4,2];
arr_players[12] = ['Horea Profis',19,112,2,5,104,185,3,99,2,2,3,1,2];
arr_players[13] = ['Iuliu Alexandrescu',19,112,1,4,99,174,3,99,1,2,4,1,2];
arr_players[14] = ['Ionut Halipa',20,117,2,4,87,171,2,99,4,4,2,1,5];
arr_players[15] = ['Ioan Ogasanu',20,120,1,5,85,198,4,99,4,4,3,3,3];
arr_players[16] = ['Larie Lozovan',19,115,1,5,102,180,3,99,4,4,2,1,2];
arr_players[17] = ['Mugur Buscan',20,114,1,3,93,192,2,99,1,2,2,3,5];
arr_players[18] = ['Nicusor Lucescu',22,113,2,4,94,193,4,99,2,2,2,3,1];
arr_players[19] = ['Olimpu Plesu',25,117,1,4,90,177,3,99,1,3,2,4,5];
arr_players[20] = ['Razvan Corlatean',23,112,1,1,92,194,1,99,2,3,4,1,2];
arr_players[21] = ['Sorin Halmageanu',21,115,1,4,101,193,2,99,2,2,2,3,5];
arr_players[22] = ['Sebi Ioanovici',21,116,1,3,104,179,2,99,4,3,2,3,3];
arr_players[23] = ['Titu Baldovin',25,113,2,2,100,185,2,99,3,3,2,2,2];
function sortMultiDimensional(a,b) {
    if (arr_ord[col] == 'asc')
        return ((a[col] &lt; b[col]) ? -1 : ((a[col] &gt; b[col]) ? 1 : 0));
    else
        return ((a[col] &gt; b[col]) ? -1 : ((a[col] &lt; b[col]) ? 1 : 0));
}
function sort_arr(crit) {
    for (i=0; i&lt;arr_head.length; i++) {
        if (arr_head[i] == crit ) {
            col = i;
            arr_players.sort(sortMultiDimensional);
        }
    }
    var tbl = document.getElementById('players').getElementsByTagName('tr');
    for (i=0; i&lt;arr_players.length; i++) {
            linie = i + 2;
            tbl[linie].getElementsByTagName('td')[1].innerHTML = arr_players[i][1];
    }
}
&lt;/script&gt;

&lt;table id="players" border="0" width="100%"&gt;
    &lt;tr&gt;
        &lt;th style="text-align: center;"&gt;&lt;?php echo $text['PLAYERS']; ?&gt;&lt;/th&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
        &lt;td&gt;
            &lt;select onchange="sort_arr(this.options[selectedIndex].value)"&gt; 
                &lt;option&gt;&lt;/option&gt;
                &lt;option value="name"&gt;&lt;?php echo $text['NAME']; ?&gt;&lt;/option&gt; 
                &lt;option value="age"&gt;&lt;?php echo $text['AGE']; ?&gt;&lt;/option&gt; 
                &lt;option value="ss"&gt;&lt;?php echo $text['SS']; ?&gt;&lt;/option&gt; 
                &lt;option value="experience"&gt;&lt;?php echo $text['EXPERIENCE']; ?&gt;&lt;/option&gt;
                &lt;option value="leadership"&gt;&lt;?php echo $text['LEADERSHIP']; ?&gt;&lt;/option&gt;              
                &lt;option value="weight"&gt;&lt;?php echo $text['WEIGHT']; ?&gt;&lt;/option&gt;              
                &lt;option value="height"&gt;&lt;?php echo $text['HEIGHT']; ?&gt;&lt;/option&gt;      
                &lt;option value="stamina"&gt;&lt;?php echo $text['STAMINA']; ?&gt;&lt;/option&gt;                
                &lt;option value="strength"&gt;&lt;?php echo $text['STRENGTH']; ?&gt;&lt;/option&gt;              
                &lt;option value="breakthroughs"&gt;&lt;?php echo $text['BREAKTHROUGHS']; ?&gt;&lt;/option&gt; 
                &lt;option value="tackling"&gt;&lt;?php echo $text['TACKLING']; ?&gt;&lt;/option&gt;
                &lt;option value="handling"&gt;&lt;?php echo $text['HANDLING']; ?&gt;&lt;/option&gt;              
                &lt;option value="speed"&gt;&lt;?php echo $text['SPEED']; ?&gt;&lt;/option&gt; 
                &lt;option value="kicking"&gt;&lt;?php echo $text['KICKING']; ?&gt;&lt;/option&gt; 
            &lt;/select&gt;
        &lt;/td&gt;
    &lt;/tr&gt;   
    &lt;tbody&gt;
        &lt;?php for($i=0; $i&lt;count($arr_players); $i++) { ?&gt;
            &lt;tr&gt;&lt;td&gt;&lt;?php echo $arr_players[$i][1]; ?&gt;&lt;/td&gt;&lt;/tr&gt;
        &lt;?php } ?&gt;  
    &lt;/tbody&gt;
&lt;/table&gt; 
</code></pre>

<p>But it doesn't work. What is wrong?</p>
