数组对象通过属性值⼤⼩排序sort() ⽅法:
var arr =[{name:'Sharon',age:18},{name;'Kris', age:20}];
var compare=function(prop){
降糖草药
清肠胃return function(a, b){
var val1 = a[prop];
var val2 = b[prop];
if(val1 < val2){
return-1;
}el if(val1 > val2){
return1;
}el{
return0;
}
}
}
狼吞虎咽造句arr.sort(compare('age'));
鸡眼是怎么长出来的// 如果age的值是字符串
var arr =[{name:'Sharon',age:'18'},{name;'Kris', age:'20'}];
var compare=function(prop){
return function(a, b){
var val1 = a[prop];
var val2 = b[prop];
if(!isNaN(Number(val1))&&!isNaN(Number(val2))){
val1 =Number(val1);
自主入睡的意思val2 =Number(val2);
}
if(val1 < val2){
return-1;
山东大专
}el if(val1 > val2){
return1;
}el{
return0;
}
}
}
多属性值进⾏排序
arr.sort(function(a, b){
if(a['ordinate']=== b['ordinate']){
北京到北戴河if(a['abscissa']> b['abscissa']){
return1
}el if(a['abscissa']< b['abscissa']){
return-1
新婚致词
}el{
return0
}
}el{
if(a['ordinate']> b['ordinate']){
return1
}el{
return-1
}
}
})