对象数组按属性排序

var str=[
    {title:"a",value:50},
    {title:"b",value:20},
    {title:"c",value:40},
    {title:"d",value:30},
];
function compare(key){
    return function(value1,value2){
        var val1=value1[key];
        var val2=value2[key];
        return val1-val2;
    }
}
str.sort(compare('value'));
console.log(str);

posted @ 2020-09-08 11:03  键盘敲飞  阅读(146)  评论(0编辑  收藏  举报