数组对象排序的JS方法

//数组对象方法排序:
    function sortByKey(array,key){
    return array.sort(function(a,b){
        var x=a[key];
        var y=b[key];
        return ((x1:((x>y)?1:0));
    });
}                                                                                 
 
  

eg:

students:[

{name:'张三',age:25},

{name:'李四',age:12},

{name:'刘五',age:35},

]

sortByKey(this.students,'age') == 结果:students:[

{name:'李四',age:12},

{name:'张三',age:25},

{name:'刘五',age:35},

]

你可能感兴趣的:(数组对象排序的JS方法)