数组根据对象的属性重新排序

      const list = [
        {id:1,age:44,index: 3}, 
        {id:2,age:32,index: 1}, 
        {id:3,age:22,index: 4}, 
        {id:4,age:26,index: 2}, 
      ]
      const compare = key => {
        return (value1, value2) => {
          const index1 = value1[key]
          const index2 = value2[key]
          return index1 - index2
        }
      }
      const list2 = list.sort(compare('age'))
      const list3 = list.sort(compare('index'))
posted @ 2022-03-24 22:31  柯宝宝智商感人  阅读(33)  评论(0)    收藏  举报