json数组排序

/*对象*/
    function JsonSort(obj, field, sortby) {
        this.obj = obj;
        this.field = field;
        this.sortby = sortby;
    }

    JsonSort.prototype.sort= function() {
        var $this=this;
        var ascend = function(a, b) {
            return a[$this.field] > b[$this.field] ? 1 : -1;
        };
        var descend = function(a, b) {
            return a[$this.field] > b[$this.field] ? -1 : 1;
        };
        if (this.sortby == "ascend") {
            this.obj.sort(ascend);
        } else {
            this.obj.sort(descend);
        }
    };
    var json = [{
        name: 'kavan',
        age: 25
    }, {
        name: 'elle',
        age: 24
    }, {
        name: 'jek',
        age: 22
    }];

    var jsonSort=new JsonSort(json,'age','ascend');
    jsonSort.sort();
    console.log(json);
原文地址:https://www.cnblogs.com/yexiangwang/p/5563714.html