sort在项目中不起作用的情况

sort方法定义看此处

JavaScript数组排序之sort函数 - 知乎 (zhihu.com)

我自己的项目里有这一段,表格内容排序,之前用的sort方法不起作用,表格内数据并没有变化,实际上是prop的关键词没选,按照上面知乎案例选好关键词就行了

sortFun(column) {
      this.colType = column.prop;
      console.log(this.colType);

      function sortby(prop, rev = true) {
        // prop 属性名
        // rev  升序降序 默认升序
        return function (a, b) {
          var val1 = a[prop];
          var val2 = b[prop];
          return rev ? val1 - val2 : val2 - val1;
        };
      }

      switch (column.order) {
        case "descending":
          console.log("des");
          this.tempData.sort(sortby(this.colType,false));
          break;
        case "ascending":
          console.log("asc");
          this.tempData.sort(sortby(this.colType));
          break;
        default:
          break;
      }

      let temp = this.tempData.map((key) => {
        console.log(key[this.colType]);
      });
    },

  

人生到处知何似,应似飞鸿踏雪泥。
原文地址:https://www.cnblogs.com/lepanyou/p/15070616.html