<script>
function p(name,age){
this.name = name;
this.age = age;
}
var p1 = new p('e',22);
var p2 = new p('s',21);
var p3 = new p('b',33);
var ps = [p1,p2,p3];
function bbb(atr){
function ddd(obj1,obj2){
if(obj1[atr]>obj2[atr]){
return 1;
}else if(obj1[atr] ==obj2[atr]){
return 0;
}else{
return -1;
}
}
return ddd;
}
ps.sort(bbb('name'));
function show(){
var h = document.getElementById('h');
for(var i in ps){
h.innerHTML += ps[i].name+'***'+ps[i].age+"<br>";
}
}
show();
</script>
为什么我把obj1[atr] > obj2[atr] 改 成 obj1.atr > obj2.atr后就无法比较大小了;
stone310
相关分类