zoukankan      html  css  js  c++  java
  • js多字段排序

    function sortByProps(item1, item2, obj) {
      // 多字段排序  
      //asc升序 desc 降序
      var props = [];
      if (obj) {
          props.push(obj);
      }
      var cps = [], // 存储排序属性比较结果。
          // 如果未指定排序属性(即obj不存在),则按照全属性升序排序。
          // 记录下两个排序项按照各个排序属性进行比较得到的结果
          asc = true;
    
      function handleNull(val) {
          return val === '' || val === null;
      }
      if (props.length < 1) {
          for (var p in item1) {
              let a1, b1;
              a1 = item1[p];
              b1 = item2[p];
              if (handleNull(a1) && handleNull(b1)) {
                  cps.push(0);
              } else if (handleNull(a1) && !handleNull(b1)) {
                  cps.push(1);
                  break;
              } else if (!handleNull(a1) && handleNull(b1)) {
                  cps.push(-1);
                  break; // 小于时跳出循环。
              } else {
                  if (a1 > b1) {
                      cps.push(1);
                      break; // 大于时跳出循环。
                  } else if (a1 === b1) {
                      cps.push(0);
                  } else {
                      cps.push(-1);
                      break; // 小于时跳出循环。
                  }
              }
          }
      } else {
          for (var i = 0; i < props.length; i++) {
              var prop = props[i];
              for (var o in prop) {
                  asc = prop[o] === 'asc';
                  let a2, b2;
                  a2 = item1[o];
                  b2 = item2[o];
                  if (handleNull(a2) && handleNull(b2)) {
                      cps.push(0);
                  } else if (handleNull(a2) && !handleNull(b2)) {
                      cps.push(asc ? 1 : -1);
                      break; // 大于时跳出循环。
                  } else if (!handleNull(a2) && handleNull(b2)) {
                      cps.push(asc ? -1 : 1);
                      break; // 小于时跳出循环。。
                  } else {
                      if (a2 > b2) {
                          cps.push(asc ? 1 : -1);
                          break; // 大于时跳出循环。
                      } else if (a2 === b2) {
                          cps.push(0);
                      } else {
                          cps.push(asc ? -1 : 1);
                          break; // 小于时跳出循环。。
                      }
                  }
              }
          }
      }
      // 根据各排序属性比较结果综合判断得出两个比较项的最终大小关系
      for (var j = 0; j < cps.length; j++) {
          if (cps[j] === 1 || cps[j] === -1) {
              return cps[j];
          }
      }
      return false;
    }
    
    
    //示例
    data.sort(function(a, b) {
      return sortByProps(a, b, {
          productName: 'asc',
          id: 'asc',
      });
    });
    

      

  • 相关阅读:
    Java分层经验
    appium自动化测试之H5页面测试
    appium自动化测试日志收集-logging
    mysql查询实践题
    python每日一练之单元测试
    python每日一练之I/O实现读写csv文件
    python每日一练之读写txt文件
    selenium定位元素后,click不生效的问题。
    selenium自动化测试之浏览器滚动条
    selenium自动化测试之单选、下拉列表、alert弹窗处理、页面刷新
  • 原文地址:https://www.cnblogs.com/lzq035/p/14338891.html
Copyright © 2011-2022 走看看