JavaScript对json对象数组排序(按照某个属性升降序排列)

     var data = [{
                name: "海外事业部",
                value: 0.58
            }, {
                name: "内销",
                value: 0.36
            }, {
                name: "互联网中心",
                value: 0.78
            }];  

           //定义一个比较器--降序排列
            function compare(propertyName) {
                return function(object1, object2) {
                    var value1 = object1[propertyName];
                    var value2 = object2[propertyName];
                    if(value2 < value1) {
                        return -1;
                    } else if(value2 > value1) {
                        return 1;
                    } else {
                        return 0;
                    }
                }
            }

           data.sort(compare("value"));

data即为排序后的数据。

posted @ 2018-04-26 17:08  高木子  阅读(477)  评论(0编辑  收藏  举报