js中对象数组按对象属性排序

let arr = [
  {
    id: 1227,
    isRead: true,
    commentid: 9,
    lectureinfoid: null,
    name: '张三',
    newsId: 26,
    readDate: null,
    sendDate: '2020-07-22 16:18:42',
    standby1: null,
    standby2: null,
    title: '站内信',
    toServiceId: 5,
    tyepe: null,
    type: true,
    userId: null
  },
  {
    addinfotimeStr: null,
    comimg: null,
    commentid: 15,
    commentinfo: '好好学习',
    commenttime: null,
    comsList: null,
    defaultImg: null,
    gender: 1,
    headImg: 'http://xcxqny.yhy.ren/xiaochengxu/toxiangnan.png',
    id: 5510,
    isExceed: null,
    isRead: true,
    is_like: null,
    lectureinfoid: 669,
    like_num: null,
    name: '李四',
    pid: 731,
    sendDate: '2020-10-23 16:20:34',
    status: 1,
    stop_smoking_day: null,
    toServiceId: 6,
    usercode: '10128'
  },
  {
    addinfotimeStr: null,
    comimg: null,
    commentid: 6,
    commentinfo: '好好学习',
    commenttime: null,
    comsList: null,
    defaultImg: null,
    gender: 1,
    headImg: 'http://xcxqny.yhy.ren/xiaochengxu/toxiangnan.png',
    id: 5510,
    isExceed: null,
    isRead: true,
    is_like: null,
    lectureinfoid: 669,
    like_num: null,
    name: '王五',
    pid: 731,
    sendDate: '2020-10-23 16:20:32',
    status: 1,
    stop_smoking_day: null,
    toServiceId: 6,
    usercode: '10128'
  },
  {
    addinfotimeStr: null,
    comimg: null,
    commentid: 4,
    commentinfo: '遇到平台期了吗?',
    commenttime: null,
    comsList: null,
    defaultImg: null,
    gender: 2,
    headImg: 'http://xcxqny.yhy.ren/xiaochengxu/toxiangnv.png',
    id: 5726,
    isExceed: null,
    isRead: true,
    is_like: null,
    lectureinfoid: 669,
    like_num: null,
    name: '陈六',
    pid: 798,
    sendDate: '2020-11-09 08:45:38',
    status: 1,
    stop_smoking_day: null,
    toServiceId: 6,
    usercode: '10083'
  }
]
/**
 * 数组元素通过指定属性进行比较
 * @param {String} property 属性名称
 * @param {String} sortType 排序方式,默认为升序 asc 排列
 * @example 使用时传入 Array.sort 方法中
 *    arr.sort(arrayCompare('commentid'))
 *    arr.sort(arrayCompare('commentid', 'desc'))
 */
const arrayCompare = (property, sortType = 'asc') => {
  /**
   * 比较函数
   * @param {Object} next 数组中下一个元素
   * @param {Object} curr 数组中当前一个元素
   */
  return function (next, curr) {
    let result = false

    // 获取下一个元素
    const nextValue = next[property]
    // 获取当前元素
    const preValue = curr[property]

    // 升序排列
    if (sortType === 'asc') {
      // 日期
      if (isNaN(preValue) && !isNaN(Date.parse(preValue))) {
        result = new Date(nextValue) - new Date(preValue)
      } else {
        result = nextValue - preValue
      }
    } else if (sortType === 'desc') {
      // 日期
      if (isNaN(preValue) && !isNaN(Date.parse(preValue))) {
        result = new Date(preValue) - new Date(nextValue)
      } else {
        result = preValue - nextValue
      }
    }

    return result
  }
}
console.log(
  '按 sendDate 降序排序前:',
  JSON.stringify(
    arr.map(x => {
      return {
        id: x.id,
        sendDate: x.sendDate
      }
    })
  )
)
arr = arr.sort(arrayCompare('sendDate', 'desc'))
console.log(
  '按 sendDate 降序排序后:',
  JSON.stringify(
    arr.map(x => {
      return {
        id: x.id,
        sendDate: x.sendDate
      }
    })
  )
)
console.log('
')
console.log(
  '按 commentid 降序排序前:',
  JSON.stringify(
    arr.map(x => {
      return {
        id: x.id,
        commentid: x.commentid
      }
    })
  )
)
arr = arr.sort(arrayCompare('commentid', 'desc'))
console.log(
  '按 commentid 降序排序后:',
  JSON.stringify(
    arr.map(x => {
      return {
        id: x.id,
        commentid: x.commentid
      }
    })
  )
)
原文地址:https://www.cnblogs.com/jardeng/p/13986325.html