数组对象排序sort

2018-09-21  本文已影响35人  天字一等
var data = [{name : "Zachary", age:28},{name:"Nicholas",age:29}];
function createComparisonFunction(propertyName){
    return function(object1,object2){
        var value1 = object1[propertyName];
        var value2 = object2[propertyName];
        if (value1 < value2){
            return -1;
        }else if(value1 > value2){
            return 1;
        }else{
            return 0;
        }
    };
}
data.sort(createComparisonFunction("name"));
alert(data[0].name)

上一篇下一篇

猜你喜欢

热点阅读