var asc = 1;
function sortTable(tId,columnIndex){
var oTable;
if(typeof(tId)=="object"){ oTable = tId;}
else{ oTable = document.getElementById(tId);}
if(!oTable){ return false;}
var emptyRows = 1;
for(var i = 1 ; i < oTable.rows.length ; i++)
{
for(var j=1;j< oTable.rows(i).cells.length;j++)
{
if(oTable.rows(i).cells[j].innerText.replace(/(\s*$)/g, "") != "")
{
emptyRows ++;
break;
}
}
}
var startRow = 0,endRow = emptyRows;
if(arguments[2]){ startRow = arguments[2];}
if(arguments[3]){ endRow = arguments[3];}
if(endRow<2){ return false;}
var col = [];
for(var r = startRow; r < endRow; r++)
{
_key = "";
if(oTable.rows(r).cells.length > columnIndex)
_key = oTable.rows(r).cells[columnIndex].innerText;
col.push({key:_key,value:oTable.rows(r).cloneNode(true)});
}
col = col.sort(sortKey);
function sortKey(p1,p2){
if(p1.key > p2.key)
return 1 * asc;
else(p1.key < p2.key)
return -1 * asc;
return 0;
}
for(var r = 0; r < col.length; r++)
{
oTable.rows(r + startRow).replaceNode(col[r].value);
}
for(var i = 1 ; i < oTable.rows.length ; i++)
{
oTable.rows[i].cells[0].innerHTML = i;
}
asc = -asc;
return true;
}
tid为table的id,columnIndex为从哪一行开始排序