var data = [{
    name: "zhao",
    age: 22
  }, {
    name: "qian",
    age: 21
  }, {
    name: "sun",
    age: 25
  }];
  //定义一个比较器
  function compare(propertyName,asc) {
    return function(a,b){  
           return asc?  ~~(a[propertyName] > b[propertyName]):~~(a[propertyName] < b[propertyName]) ;  
    }  
  }
  //使用方法
  data.sort(compare("age"));
  console.log(data);

 

相关文章:

  • 2021-07-02
  • 2022-01-15
  • 2022-12-23
  • 2021-06-02
  • 2022-12-23
  • 2021-11-20
  • 2022-12-23
  • 2021-09-10
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案