Forums

This topic is locked

Client Side Tablesorting

Posted 18 Oct 2004 18:44:17
1
has voted
18 Oct 2004 18:44:17 Jelle-Jacob de Vries posted:
How can I modify the following script for use with date's and not rounded integers like 1, 5 etc....

<!--
var sortedOn = 0;
function SortTable(id, sortOn) {

var table = document.getElementById(id);
var tbody = table.getElementsByTagName('tbody')[0];
var rows = tbody.getElementsByTagName('tr');

var rowArray = new Array();
for (var i=0, length=rows.length; i<length; i++) {
rowArray[i] = rows[i].cloneNode(true);
}

if (sortOn == sortedOn) { rowArray.reverse(); }
else {
sortedOn = sortOn;
if (sortedOn == 0) {
rowArray.sort(RowCompareNumbers);
}
else if (sortedOn == 3) {
rowArray.sort(RowCompareDollars);
}
else {
rowArray.sort(RowCompare);
}
}

var newTbody = document.createElement('tbody');
for (var i=0, length=rowArray.length; i<length; i++) {
newTbody.appendChild(rowArray[i]);
}

table.replaceChild(newTbody, tbody);
}

function RowCompare(a, b) {

var aVal = a.getElementsByTagName('td')[sortedOn].firstChild.nodeValue;
var bVal = b.getElementsByTagName('td')[sortedOn].firstChild.nodeValue;
return (aVal == bVal ? 0 : (aVal > bVal ? 1 : -1));
}

function RowCompareNumbers(a, b) {

var aVal = parseInt(a.getElementsByTagName('td')[sortedOn].firstChild.nodeValue);
var bVal = parseInt(b.getElementsByTagName('td')[sortedOn].firstChild.nodeValue);
return (aVal - bVal);
}

function RowCompareDollars(a, b) {

var aVal = parseFloat(a.getElementsByTagName('td')[sortedOn].firstChild.nodeValue.substr(1));
var bVal = parseFloat(b.getElementsByTagName('td')[sortedOn].firstChild.nodeValue.substr(1));
return (aVal - bVal);
}
//-->

Reply to this topic