效果
- 结合数组的索引方法及 sort 方法,按照既定顺序(非升序或降序)排序
- 如下示例,type 属性按照 3 → 4 → 2 → 1 → 5 的顺序进行排序
代码
getList() {this.tableData = [];axios.get("/list2").then((res) => {console.log("res", res);if (res.status == 200) {this.tableData = res.data;this.tableSortData = this.sortDataFunc(this.tableData);console.log(this.tableData, this.tableSortData);}});
},
sortDataFunc(totalArr) {let resultArr = [];let _this = this;resultArr = totalArr.filter((item) => {return _this.sortArr.includes(item.type);});function getIndex(str) {return _this.sortArr.indexOf(str);}resultArr.sort((a, b) => {return getIndex(a.type) - getIndex(b.type);});return resultArr;
},