jscript 利用 array.sort() 和 nodes.swap()
來完成重新排列。
但速度比xml的sortnode慢。(-_-メ)
<!--
http://dwin.net
copyright(c) 1998-2004 dewin all rights reserved
start 2003-09-18 15:50
finish 2003-09-18 16:17
-->
<body>
<script>
/* create the table */
trlength = 100
letter="abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789"
str = "\
<table id=test style=''>\
<tbody>\
<tr onclick='if(event.srcelement!=this)sortnode(event.srcelement)'><td style='background:#ff69b4'>letter</td><td style='background:#adff2f'>name</td><td style='background:#87cefa'>money</td></tr>\
</tbody>\
<tbody>"
for(i=0;i<trlength;i++)
str += "<tr><td>"+letter.charat(math.floor(math.random()*10000%62))+"</td><td>"+string.fromcharcode(math.round(math.random()*(40869-19968)+19968))+"</td><td>"+string(math.random()).slice(2)+"</td></tr>"
str += "</tbody>\
</table>"
document.write(str)
ary = [];
otr = test.childnodes[1].childnodes;
otrorder = test.firstchild.firstchild.childnodes;
for(i=0, j=otr.length; i<j; i++)//collect tr nodes in array
ary[i] = otr[i];
for(i=0, j=otrorder.length; i<j; i++)
otrorder[i].order = -1;
function sortnode(sortnode){
var time = new date()
var tempa, tempb, otd, orders
otd = sortnode.sourceindex-sortnode.parentelement.sourceindex-1;//which td we click
orders = sortnode.order = -sortnode.order;
ary.sort(//sort the nodes here.
function(a,b){
tempa = a.childnodes[otd].innertext;//you can use a.childnodes[otd].innertext.length or other things to sort
tempb = b.childnodes[otd].innertext;
temp = tempa<tempb?-orders:tempa==tempb?0:orders;
return temp;
}
)
for(i=0; i<otr.length; i++)
otr[i].swapnode(ary[i]);//swap tr nodes
alert(new date()-time);
}
</script>