javascript sort rows by column
<p>I have the following function which has a problem in that after inserting the rows back into the table, they appear empty when inspecting with firebug. Hopefully it's a simple fix to this code:</p>

<pre><code>////////////////////////////////////////////////////////////////////////////////////////
// Desc: Sorts a summary_table table by the column name. 
// Args: column_name - the column to sort by
// Ret : None - changes the dom.
////////////////////////////////////////////////////////////////////////////////////////
function sortBy(column_name)
{
    var table_elements = document.getElementsByClassName("summary_table");
    var table_element = table_elements[0];
    var table_rows = table_element.rows;

    var sorted_array = new Array();
    var rows_to_put_back = new Array();
    for (index in table_rows)
    {
        var table_row = table_rows[index];

        if (table_row instanceof HTMLTableRowElement == true)
        {
            var row_id = table_row.getAttribute('name');

            if (row_id != "table_header")
            {
                rows_to_put_back[index-1] = table_row;
                var cells = table_row.cells;
                var relevant_value = "";

                for (s in cells)
                {
                    var tmp_cell = cells[s];
                    var cell_name = tmp_cell.getAttribute('name');
                    if (cell_name == column_name)
                    {
                        relevant_value = tmp_cell.innerHTML;
                        break;
                    }
                }

                if (relevant_value != "")
                {
                    sorted_array[row_id] = relevant_value;
                }
                else
                {
                    alert("Did not find the necessary column!");
                }
            }
            else
            {
                var header_row = table_row;
            }
        }
    }

    // Remove the rows from the table so they can be put back on in order.
    // Remove one from num_iterations because we dont remove the header row which has col names.
    var num_iterations = table_rows.length - 1;
    for (var s=0; s&lt;num_iterations; s++)
    {
        // Remove row at index 1 because index 0  is the header row.
        table_element.deleteRow(1);
    }

    // Sort the array, so it now is actually a sorted array.
    sorted_array.sort();

    for (index in sorted_array)
    {
        row_id = index;
        row_to_put_on = rows_to_put_back[row_id];

        alert(row_to_put_on);
        var row = table_element.insertRow(1);
        row = row_to_put_on;

    }
}
</code></pre>
