JavaScript 根据数组中对象的属性排序

来源:互联网 发布:百度云同步盘官网 mac 编辑:程序博客网 时间:2024/04/29 21:43

 

根据name排序:

 var test  = new Array();
test.push({id:1,name:"walker",order:3});
test.push({id:2,name:"david",order:2});
test.push({id:3,name:"fei",order:1});


test.sort(function(a,b){
    return a.name.localeCompare(b.name);
});

 

根据order排序:
test.sort(function(a,b){
    return a.order -b.order;
});

原创粉丝点击