html部分:
<button id="asc">asc</button>
<button id="des">des</button>
<table></table>
js部分:
var arrayOne=[{name:"aaa",age:22,height:160},{name:"bbb",age:23,height:161},{name:"ccc",age:21,height:159}];
$("#asc").on("click",function(){
arrayOne.sort(compare("asc","name"));
$("table").html("");
for(var i=0;i<arrayOne.length;i++){
$("table").append("<tr><td>"+arrayOne[i].name+"</td><td>"+arrayOne[i].age+"</td><td>"+arrayOne[i].height+"</td></tr>")
}
});
$("#des").on("click",function(){
arrayOne.sort(compare("des","name"));
$("table").html("");
for(var i=0;i<arrayOne.length;i++){
$("table").append("<tr><td>"+arrayOne[i].name+"</td><td>"+arrayOne[i].age+"</td><td>"+arrayOne[i].height+"</td></tr>")
}
});
function compare(order,sortBy){
var oldAlpah=(order=="asc")?">":"<";
var sortFun=new Function("a","b","return a."+sortBy+oldAlpah+"b."+sortBy+"?1:-1");
return sortFun;
}
新闻热点
疑难解答