根据对象的某个属性名的值从新排序

var list=[
    {
        name:"aaa",
        score:80
    },
    {
        name:"bbb",
        score:70
    },
    {
        name:"ccc",
        score:90
    },
    {
        name:"ddd",
        score:60
    }
];
function compareFn(propertyName){
    return function(obj1,obj2){
        var value1 = obj1[propertyName];
        var value2 = obj2[propertyName];
        if(value1 < value2){
            //return 1 //max-min
            return -1 //min-max
        }else if(value1 > value2){
            //return -1;//max-min
            return 1;// min-max
        }else{
            return 0;
        }
    }
}
list.sort(compareFn('score'));//-1,从min-max排序,1 从max-min

 

 

posted @ 2018-06-11 14:23  逗伴不是瓣  阅读(482)  评论(0编辑  收藏  举报