现在的位置: 首页 > 综合 > 正文

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

2014年09月05日 ⁄ 综合 ⁄ 共 275字 ⁄ 字号 评论关闭

 

根据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;
});

抱歉!评论已关闭.