jquery 排序table


var i = 0;

$(document).ready(myFunction);

function myFunction()
{
$('<table id="table" width="100" height="100" border="1"></table>')
.appendTo('body')
.bind('click',myclick);

$("#sortASC").click(function(){

SortArray.array = $("#table tr");

SortArray.sortAsc(0,$("#table tr").length-1);

$("#table tr").detach();

$.each(SortArray.array,function(){
$("#table").append(this);
})
});

$("#sortDESC").click(function(){

SortArray.array = $("#table tr");

SortArray.sortDesc(0,$("#table tr").length-1);

$("#table tr").detach();

$.each(SortArray.array,function(){
$("#table").append(this);
})
});
}
function myclick(){
$('<tr id="'+(i++)+'"><td>'+(i)+'</td></tr>')
.appendTo($(this));
}

var SortArray={
array:[],
sortDesc:function(low,high){
if(low<high){
var middle = this.getMiddleDESC(low,high);
this.sortDesc(0,middle -1);
this.sortDesc(middle +1,high);
}
},
sortAsc: function(low,high){
if(low<high){
var middle = this.getMiddleASC(low,high);
this.sortAsc(0,middle -1);
this.sortAsc(middle +1,high);
}
},
getMiddleDESC:function(low,high){
var temp = this.array[low];
while(low<high){

while(low<high && parseInt($(this.array[low]).attr("id")) > parseInt($(this.array[high]).attr("id"))){
high--;
}
this.array[low] = this.array[high];

while(low<high && parseInt($(this.array[low]).attr("id")) <= parseInt($(this.array[low]).attr("id"))){
low++;
}
this.array[high] = this.array[low];
}
this.array[low] = temp;
return low;
},
getMiddleASC: function(low,high){
var temp = this.array[low];
while(low<high){

while(low<high && parseInt($(this.array[low]).attr("id")) < parseInt($(this.array[high]).attr("id"))){
high--;
}
this.array[low] = this.array[high];

while(low<high && parseInt($(this.array[low]).attr("id")) >= parseInt($(this.array[low]).attr("id"))){
low++;
}
this.array[high] = this.array[low];
}
this.array[low] = temp;
return low;
}
}



<div class="clickme">Click here</div>
<input id="sortASC" type="button" value="升序" />
<input id="sortDESC" type="button" value="降序" />
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值