JS对Map类型数据进行排序

与后台配合中会遇到数据处理的问题,对于Map类型的数据要做排序,一直没有找到合理的方案,这里展示的比较粗暴

例子:
这个是Map数据
在这里插入图片描述
现在要对他做排序,从大到小
直接上代码,可以复制在本地验证,大体思路就是这样,然后看看如何变化应用到实际的项目中去

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      var mapList = {
        20200312: [
          {
            key1: "000",
            key2: "111",
          },
          {
            key1: "000",
            key2: "111",
          },
        ],
        20200310: [
          {
            key1: "111",
            key2: "111",
          },
          {
            key1: "111",
            key2: "111",
          },
          {
            key1: "111",
            key2: "111",
          },
        ],
        20200314: [
          {
            key1: "222",
            key2: "111",
          },
          {
            key1: "222",
            key2: "111",
          },
          {
            key1: "222",
            key2: "111",
          },
        ],
      }
     

      var keyList = []
      for (var key in mapList) {
        keyList.push(key)
      }
      keyList.sort(function (key1, key2) {
        return key2 - key1
      })
      var resultList = new Map()
      for (var i = 0; i < keyList.length; i++) {
        console.log(keyList[i])
        resultList.set(keyList[i], mapList[keyList[i]])
      }
      console.log(resultList, 4444)
    </script>
  </body>
</html>

结果:
JS对Map类型数据进行排序_第1张图片

你可能感兴趣的:(Js,javascript,前端)