JS数组针对某键的值进行升序和降序

    dictArraySort (dictArray, sortKey, sortType="ascending", isTime = false) {
      if (!isTime) {
        if (sortType == 'ascending') {
          dictArray.sort(function (dictA, dictB) {
            return dictA[sortKey] - dictB[sortKey]
          })
        }
        if (sortType == 'descending') {
          dictArray.sort(function (dictA, dictB) {
            return dictB[sortKey] - dictA[sortKey]
          })
        }
      } else {
        // 如果是日期时间排序
        if (sortType == 'ascending') {
          dictArray.sort(function (dictA, dictB) {
            return Date.parse(dictA[sortKey].replace(/-/g,"/"))-Date.parse(dictB[sortKey].replace(/-/g,"/"))
          })
        }
        if (sortType == 'descending') {
          dictArray.sort(function (dictA, dictB) {
            return Date.parse(dictB[sortKey].replace(/-/g,"/"))-Date.parse(dictA[sortKey].replace(/-/g,"/"))
          })
        }
      }
      return dictArray
    },    

 

posted @ 2022-10-25 17:50  rachelch  阅读(371)  评论(0编辑  收藏  举报