对象数组根据对象中某一元素进行排序

以时间排序为例

知识点:js原生数组排序 Array.sort(function(){})
这种方法会改变原数组,无需return

//处理函数
handleSort(arr){
    arr.sort((a,b)=>{
        let t1 = new Date(Date.parse(a.birthday.replace(/-/g,"/")))
        let t2 = new Date(Date.parse(b.birthday.replace(/-/g,"/")))
        return t2.getTime()-t1.getTime()
    })
},

如何使用:
let objList=[
  {title:"小红", birtyday:'2020-04-05', height:"60"},
  {title:"小明", birthday:'2020-06-06', height:"50"},
  {title:"小刚", birthday:'2020-05-05', height:"55"}
]
this.handleSort(objList)
posted @ 2021-12-22 16:12  huihuihero  阅读(263)  评论(0编辑  收藏  举报