排序代码。在示例下500条数据时,效率提高50%的得分。(代码不必拘限于示例代码)
<html>
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
<title> JK:支持民族工业,尽量少买X货 </title>
<script language= "javascript " srccccc= "CommonJS_RowSort.js ">
var rowSortImg=document.createElement( "img ");
function rowSortFun(tdObj){
var d1=new Date();
var currentColumnIndex=tdObj.cellIndex;
if(rowSortImg.parentNode != tdObj){
tdObj.appendChild(rowSortImg);
rowSortImg.src= "up.gif ";
}
else if (rowSortImg.src.indexOf( "down.gif ")> -1){
rowSortImg.src= "up.gif ";
}
else{
rowSortImg.src= "down.gif ";
}
var currentTbody=tdObj.parentNode.parentNode;
var rowsArray=new Array();
for( var i=1;i <currentTbody.rows.length;i++){
rowsArray[i-1]=currentTbody.rows[i].cells[currentColumnIndex];
}
rowsArray.sort(getSortFun(tdObj));
for(var i=0;i <rowsArray.length;i++){
currentTbody.appendChild(rowsArray[i].parentNode);
}
alert( "行数 "+rowsArray.length+ ";所花毫秒数 "+(new Date()-d1));
}
function getSortFun(tdObj){
var sortIsAsc=(rowSortImg.src.indexOf( "up.gif ")> -1);
if( "Number "==tdObj.columnContentType && sortIsAsc){
return function (trObj1,trObj2) {return trObj1.innerHTML*1-trObj2.innerHTML*1;}
}
else if( "Number "==tdObj.columnContentType && !sortIsAsc){
return function (trObj1,trObj2) { return trObj2.innerHTML*1-trObj1.innerHTML*1;}
}
else if(sortIsAsc){
return function (trObj1,trObj2) { return (trObj1.innerHTML.toUpperCase() > trObj2.innerHTML.toUpperCase())-0.5;}
}
else {
return function (trObj1,trObj2) { return (trObj1.innerHTML.toUpperCase() < trObj2.innerHTML.toUpperCase())-0.5;}
}
}
</script>
<style>
.headerTr{background-color:#cccccc;}
</style>
</head>
<body>
<div style= "font-size:10pt; ">
注1:用法:需要排序的字段头里:<td columnContentType= "Text " onclick= "rowSortFun(this); "> 。 <br/>
注2:鉴于很多提出要求前台排序的人,其实并不是真的需要前台排序,所以强列建议慎用前台排序。 <br/>
<br/> &n