Why would this Javascript code work in IE and Firefox but not in Chrome?

Viewed 121

I am sorting a table using this JQuery code. The rows of the table looks like this and when a button is clicked the hidden values become visible:

 <tr>
        <td>A-1</td>
        <td nowrap>A-2</td>
        <td>A-3</td>
 </tr>
 <tr>
        <td class="hidden">A-1</td>
        <td class="hidden" nowrap>B-1</td>
        <td class="hidden">C-1</td>
        <td class="hidden" colspan="3">
          <div id="A-row-additional-info" >
              <strong>additional info 1</strong> 
              <br/><strong>additional info 2</strong> 
              <br/><strong>additional info 3</strong> 
           </div>
        </td>
 </tr>

and the JS code I'm using to sort this is:

$('th').click(function () {
    var table = $(this).parents('table').eq(0);

    var rows = table.find('tr:gt(0)').toArray().sort(comparer($(this).index()));
    this.asc = !this.asc;
    if (!this.asc) {
        rows = table.find('tr:gt(0)').toArray().sort(comparerReverse($(this).index()));
        $(this).find('a').html('&#9662');
    } else {
        $(this).find('a').html('&#9652');
    }
    for (var i = 0; i < rows.length; i++) {
        table.append(rows[i]);
    }

});
function comparer(index) {
    return function (a, b) {
        var valA;
        var valB;
        if (getCellValue(a, index).match(/^[0-9]{1,2}[\/][0-9]{1,2}[\/][0-9]{4}[ ][0-9]{1,2}[:][0-9]{1,2}[:][0-9]{1,2}[ ][a-zA-Z]{2}$/) != null)//comparing dates
        {
            valA = new Date(getCellValue(a, index)).setMilliseconds(0);
            valB = new Date(getCellValue(b, index)).setMilliseconds(0);
        }
        else {
            valA = getCellValue(a, index);
            valB = getCellValue(b, index);
        }
        return $.isNumeric(valA) && $.isNumeric(valB) ? valA - valB : valA.localeCompare(valB);
    }
}
function comparerReverse(index) {
    return function (a, b) {
        var valA;
        var valB;
        if (getCellValue(a, index).match(/^[0-9]{1,2}[\/][0-9]{1,2}[\/][0-9]{4}[ ][0-9]{1,2}[:][0-9]{1,2}[:][0-9]{1,2}[ ][a-zA-Z]{2}$/) != null)//comparing dates
        {
            valA = new Date(getCellValue(a, index)).setMilliseconds(0);
            valB = new Date(getCellValue(b, index)).setMilliseconds(0);
        }
        else {
            valA = getCellValue(a, index);
            valB = getCellValue(b, index);
        }
        return $.isNumeric(valB) && $.isNumeric(valA) ? valB - valA : valB.localeCompare(valA);
    }
}
function getCellValue(row, index) { return $(row).children('td').eq(index).html(); }

This works perfectly fine in both IE and FireFox, but in Chrome, the hidden rows are not sorted correctly. Does anyone know why and how to fix this?

0 Answers
Related