sort排序高级:多重排序-自定义规则

四月 21, 2023 / Administrator / 3阅读 / 0评论

目前需求:不同类型信息需要首先根据时间排序,再根据某个字符串排序,且排序规则需截取字符内容进行排序,最后在根据号码排序
完整代码

var getYMDHMS = function (timestamp) {
     let time = new Date(timestamp)
     let year = time.getFullYear()
     let month = time.getMonth() + 1
     let date = time.getDate()
     let hours = time.getHours()
     let minute = time.getMinutes()
     let second = time.getSeconds()

     if (month < 10) { month = '0' + month }
     if (date < 10) { date = '0' + date }
     if (hours < 10) { hours = '0' + hours }
     if (minute < 10) { minute = '0' + minute }
     if (second < 10) { second = '0' + second }
     return year + '-' + month + '-' + date
   }
   var resultAll = {
     "roles": null,
     "permissions": null,
     "success": true,
     "message": "查询成功",
     "code": "200",
     "data": {
       "pageNo": 1,
       "pageSize": 30,
       "totalCount": 4639,
       "validCount": 4639,
       "invalidCount": 380,
       "totalPage": 155,
       "searchText": "",
       "sortField": null,
       "sortOrder": "ASC",
       "outParams": {},
       "list": [

         {
           "iid": "d74085439a5c4b5e8a3b2f6423662e62",
           "title": "山东省人民政府关于免去于洪亮职务的通知",
           "orgId": "6996bb672f434d0bb82c49a8c1bd6f98",
           "createTime": 1681909854000,
           "textVersion": "[]",
           "pdfVersion": "[]",
           "issue": "鲁政任〔2023〕92号",
           "publishDate": "2023-04-19",
           "pulishDate": "2023-04-19",
           "validity": "0",
           "otherinfo1": "2",
           "publishUnit": "山东省人民政府",
           "hasExplain": "0",
           "explainId": null
         },
         {
           "iid": "7433abe63a704a11965b842c31e0e930",
           "title": "山东省人民政府关于评定赵汉语同志为烈士的批复",
           "orgId": "6996bb672f434d0bb82c49a8c1bd6f98",
           "createTime": 1681709493000,
           "textVersion": "[]",
           "pdfVersion": "[]",
           "issue": "鲁政字〔2023〕46号",
           "publishDate": "2023-04-20",
           "pulishDate": "2023-04-15",
           "validity": "0",
           "otherinfo1": "2",
           "publishUnit": "山东省人民政府",
           "hasExplain": "0",
           "explainId": null
         },
         {
           "iid": "ef4166614a774d3dbe61e61e7eca697d",
           "title": "山东省人民政府关于免去孙鹤汀职务的通知",
           "orgId": "6996bb672f434d0bb82c49a8c1bd6f98",
           "createTime": 1681910094000,
           "textVersion": "[]",
           "pdfVersion": "[]",
           "issue": "鲁政任〔2023〕96号",
           "publishDate": "2023-04-19",
           "pulishDate": "2023-04-19",
           "validity": "0",
           "otherinfo1": "2",
           "publishUnit": "山东省人民政府",
           "hasExplain": "0",
           "explainId": null
         },
         {
           "iid": "57d1688350bb4d38b948613ee33b1f46",
           "title": "山东省人民政府关于调整济南新材料产业园区等省级经济开发区规划面积的批复",
           "orgId": "6996bb672f434d0bb82c49a8c1bd6f98",
           "createTime": 1681354298000,
           "textVersion": "[]",
           "pdfVersion": "[]",
           "issue": "鲁政字〔2023〕44号",
           "publishDate": "2023-04-19",
           "pulishDate": "2023-04-11",
           "validity": "0",
           "otherinfo1": "2",
           "publishUnit": "山东省人民政府",
           "hasExplain": "0",
           "explainId": null
         },
         {
           "iid": "995b05b8627b4342b2fd768e82169ab4",
           "title": "山东省人民政府关于调整梁山经济开发区规划面积的批复",
           "orgId": "6996bb672f434d0bb82c49a8c1bd6f98",
           "createTime": 1681386692000,
           "textVersion": "[]",
           "pdfVersion": "[]",
           "issue": "鲁政字〔2023〕45号",
           "publishDate": "2023-04-19",
           "pulishDate": "2023-04-12",
           "validity": "0",
           "otherinfo1": "2",
           "publishUnit": "山东省人民政府",
           "hasExplain": "0",
           "explainId": null
         },

         {
           "iid": "84b4ce3e63374df6946f6ac56a761b08",
           "title": "山东省人民政府关于同意设立烟台城市科技职业学院的批复",
           "orgId": "6996bb672f434d0bb82c49a8c1bd6f98",
           "createTime": 1681198903000,
           "textVersion": "[]",
           "pdfVersion": "[]",
           "issue": "鲁政字〔2023〕42号",
           "publishDate": "2023-04-11",
           "pulishDate": "2023-04-09",
           "validity": "0",
           "otherinfo1": "2",
           "publishUnit": "山东省人民政府",
           "hasExplain": "0",
           "explainId": null
         },
         {
           "iid": "ef07f735af92439f9aaf913b5aa4ce4e",
           "title": "山东省人民政府办公厅关于开展主要畜禽监测调查样本轮换工作的通知",
           "orgId": "40c399d272694553bb6d38feb5cb4362",
           "createTime": 1680598892000,
           "textVersion": "[]",
           "pdfVersion": "[]",
           "issue": "鲁政办字〔2023〕36号",
           "publishDate": "2023-04-19",
           "pulishDate": "2023-04-03",
           "validity": "0",
           "otherinfo1": "2",
           "publishUnit": "山东省人民政府办公厅",
           "hasExplain": "0",
           "explainId": null
         },


         {
           "iid": "32d10b8ecb2c4cc195a7dc507798a3ad",
           "title": "山东省人民政府办公厅关于印发山东省稳定和扩大就业促增收促消费促增长行动方案的通知",
           "orgId": "40c399d272694553bb6d38feb5cb4362",
           "createTime": 1680771692000,
           "textVersion": "[]",
           "pdfVersion": "[]",
           "issue": "鲁政办发〔2023〕3号",
           "publishDate": "2023-04-19",
           "pulishDate": "2023-04-04",
           "validity": "0",
           "otherinfo1": "2",
           "publishUnit": "山东省人民政府办公厅",
           "hasExplain": "0",
           "explainId": null
         },
         {
           "iid": "f0f7af658ca449bc9f7fd2fdd750b8c3",
           "title": "山东省人民政府办公厅关于举办2023山东(青岛)RCEP国际食品饮料数字博览会的批复",
           "orgId": "40c399d272694553bb6d38feb5cb4362",
           "createTime": 1680754292000,
           "textVersion": "[]",
           "pdfVersion": "[]",
           "issue": "鲁政办字〔2023〕37号",
           "publishDate": "2023-04-19",
           "pulishDate": "2023-04-05",
           "validity": "0",
           "otherinfo1": "2",
           "publishUnit": "山东省人民政府办公厅",
           "hasExplain": "0",
           "explainId": null
         },
         {
           "iid": "055303b42c474265a8be3d68c0b422f1",
           "title": "山东省压煤村庄搬迁管理办法",
           "orgId": "6996bb672f434d0bb82c49a8c1bd6f98",
           "createTime": 1672904276000,
           "textVersion": "[{\"path\":\"jpolicy_file/filedata/2023/01/05/83f3b24189d34b578b239195d55e7de6.doc\",\"name\":\"山东省压煤村庄搬迁管理办法.doc\"}]",
           "pdfVersion": "[{\"path\":\"jpolicy_file/filedata/2023/01/05/cabc5274434841b9bc9c4a58de73508f.pdf\",\"name\":\"山东省压煤村庄搬迁管理办法.pdf\"}]",
           "issue": "山东省人民政府令第350号",
           "publishDate": "2023-04-19",
           "pulishDate": "2022-12-10",
           "validity": "0",
           "otherinfo1": "0",
           "publishUnit": "山东省人民政府",
           "hasExplain": "0",
           "explainId": null
         },

         {
           "iid": "cbb00fa84aa446db9807cf5a556c2f41",
           "title": "山东省人民政府关于调整部分县(市、区)城镇土地使用税税额标准的通知",
           "orgId": "6996bb672f434d0bb82c49a8c1bd6f98",
           "createTime": 1680313292000,
           "textVersion": "[]",
           "pdfVersion": "[]",
           "issue": "鲁政发〔2023〕3号",
           "publishDate": "2023-04-19",
           "pulishDate": "2023-03-31",
           "validity": "0",
           "otherinfo1": "2",
           "publishUnit": "山东省人民政府",
           "hasExplain": "0",
           "explainId": null
         },
         {
           "iid": "93c38bc9fcf442b39069f55d7e26933a",
           "title": "山东省人民政府办公厅关于举办2023外贸优品展销会的批复",
           "orgId": "40c399d272694553bb6d38feb5cb4362",
           "createTime": 1679991688000,
           "textVersion": "[]",
           "pdfVersion": "[]",
           "issue": "鲁政办字〔2023〕33号",
           "publishDate": "2023-03-28",
           "pulishDate": "2023-03-27",
           "validity": "0",
           "otherinfo1": "2",
           "publishUnit": "山东省人民政府办公厅",
           "hasExplain": "0",
           "explainId": null
         },
         {
           "iid": "ab0b292449a74515970a434b79d40d08",
           "title": "山东省人民政府关于委托实施部分省级行政权力事项的决定",
           "orgId": "6996bb672f434d0bb82c49a8c1bd6f98",
           "createTime": 1675064624000,
           "textVersion": "[{\"path\":\"jpolicy_file/filedata/2023/01/30/74f906f3f642431389a061170dd3abc5.docx\",\"name\":\"山东省人民政府关于委托实施部分省级行政权力事项的决定.docx\"}]",
           "pdfVersion": "[{\"path\":\"jpolicy_file/filedata/2023/01/30/ebd5e73930154d129f7baca473391f06.pdf\",\"name\":\"山东省人民政府关于委托实施部分省级行政权力事项的决定.pdf\"}]",
           "issue": "山东省人民政府令第351号",
           "publishDate": "2023-04-19",
           "pulishDate": "2022-12-26",
           "validity": "0",
           "otherinfo1": "0",
           "publishUnit": "山东省人民政府",
           "hasExplain": "0",
           "explainId": null
         },

       ]
     },
     "traceId": "a9dcff542838490e"
   }

   var search_data = [
     {
       "publishDate": "2022-11-11",
       "title": "省委、省政府印发《先进制造业强省行动计划(2022—2025年)》",
       "url": "http://www.shandong.gov.cn/art/2022/11/11/art_107860_121996.html",
       "createTime": 1681747201000
     },
     {
       "publishDate": "2023-01-03",
       "title": "省委、省政府印发《山东省建设绿色低碳高质量发展先行区三年行动计划(2023-2025年)》",
       "url": "http://www.shandong.gov.cn/art/2023/1/3/art_107860_123166.html",
       "createTime": 1681833601000
     },
     {
       "publishDate": "2022-12-23",
       "title": "省委、省政府印发贯彻落实《中共中央、国务院关于完整准确全面贯彻新发展理念做好碳达峰碳中和工作的意见》的若干措施",
       "url": "http://www.shandong.gov.cn/art/2022/12/23/art_107860_122916.html",
       "createTime": 1681747201000

     },
     {
       "publishDate": "2023-04-19",
       "title": "中共山东省委 山东省人民政府 关于促进文旅深度融合推动旅游业高质量发展的意见",
       "url": "http://www.shandong.gov.cn/art/2023/3/31/art_107860_125496.html",
       "createTime": 1681833601000
     }
   ]
   // 方法1:将政策库与jsearch信息都重组,然后先将政策库排序,之后将排完序的政策库数据与jsearch数据重新合成一个新数组按照时间排序降序排序,
   // 降序排序就要将jsarch数据在新数组中放在下面,就是后插入

   // 方法2:将政策库与jsearch信息都重组,jsearch重组中插入固定值:fwxl:生有关文件,以及fwlx_px:0,之后将jsearch数据和政策库数据重组成新数组,在进行排序
   // 建议用第二种
   var fwlx = function(obj){
     if(obj.indexOf('政府令')> -1){
       return obj.slice(0, obj.indexOf('第'))
     }else{
       return obj.slice(0, obj.indexOf('〔'))
     }
   }
   var fwlx_px = function (obj) {
     if (obj.indexOf('鲁政发') > -1) {
       return 1
     } else if (obj.indexOf('鲁政字') > -1) {
       return 2
     } else if (obj.indexOf('鲁政办发') > -1) {
       return 3
     } else if (obj.indexOf('鲁政办字') > -1) {
       return 4
     } else if (obj.indexOf('鲁政任') > -1) {
       return 5
     } else if (obj.indexOf('政府令') > -1) {
       return 6
     } else if (obj.indexOf('通告') > -1) {
       return 7
     } else {
       return 0
     }
   }
   var fwzh = function (obj) {
     if(obj.indexOf('政府令')> -1){
       return obj.slice(obj.indexOf('第') + 1, obj.indexOf('号'))
     }else{
       return obj.slice(obj.indexOf('〕') + 1, obj.indexOf('号'))
     }
   }

   resultAll = resultAll.data.list.map((item) => {
     return {
       iid: item['iid'],
       title: item['title'],
       orgId: item['orgId'],
       createTime: item['createTime'],
       issue: item['issue'],
       // 用indexOf得到下标再用slice截取
       // fwlx: item['issue'].slice(0, item['issue'].indexOf('〔')),
       fwlx: fwlx(item['issue']),
       fwlx_px: fwlx_px(item['issue']),
       // fwzh: item['issue'].slice(item['issue'].indexOf('〕') + 1, item['issue'].indexOf('号')),
       fwzh: fwzh(item['issue']),
       publishDate: item['publishDate'],
       validity: item['validity'],
       otherinfo1: item['otherinfo1'],
       publishUnit: item['publishUnit'],
       hasExplain: item['hasExplain'],
       explainId: item['explainId']
     }
   })

   search_data = search_data.map((item) => {
     return {
       publishDate: getYMDHMS(Number(item['createTime'])),
       title: item['title'],
       url: item['url'],
       fwlx_px: 0,
       fwlx: '省有关文件',
     }
   })
   var newdata = [].concat(search_data, resultAll)

   // var order = ['省有关文件', '鲁政发', '鲁政字', '鲁政办发', '鲁政办字', '鲁政任', '山东省人民政府令', '通告']
   // 可以利用order规定排序规则进行自定义排序,也可以新定义一个fwlx_px字段进行专门排序
   newdata = newdata.sort(function (a, b) {
     if (a.publishDate === b.publishDate) {
       if (a.fwlx === b.fwlx) {
         return b.fwzh - a.fwzh
       }
       // 使用自定义规则排序
       // return order.indexOf(a.fwlx) - order.indexOf(b.fwlx)
       // 使用fwlx_px字段排序
       return a.fwlx_px - b.fwlx_px
     }
     return a.publishDate < b.publishDate ? 1 : -1
   })
   console.log(newdata)
   var item = ''
   $.each(newdata, function (i, result) {
     item = "<li class='wip_col_listli'>" +
       "<a href='http://www.shandong.gov.cn/jpaas-jpolicy-web-server/front/info/detail?iid=" + result['iid'] + "' target='_blank' title='" + result['title'] + "'>" + result['title'].replace(/<.*?>/ig, "") + "</a>" +
       "<p><span class='list_fwzh'>发文字号:" + result['issue'] + "</span>" + "<span class='list_fwrq'>发布日期:" + result['publishDate'] + "</span>" + "<span class='list_fwrq'>字号排序:" + result['fwlx_px'] + "</span>" +
       "</p></li>";
     $('.pj').append(item);
   });

#work(2)

文章作者:Administrator

文章链接:http://halo.huangzunxue998.top//archives/sortpai-xu-gao-ji-duo-chong-pai-xu-zi-ding-yi-gui-ze

版权声明:本博客所有文章除特别声明外,均采用CC BY-NC-SA 4.0 许可协议,转载请注明出处!


评论