在js中我的这段代码运行时会使页面卡主,我现在找不到原因,你帮我找找有没有其他的排序算法让我的页面卡主的可能性小一点 let list50 = this.queueNameList[this.server50]; let list188 = this.queueNameList[this.server188]; console.log(this.queueSort) switch (this.queueSort) { case 'enqueueDiffAscend' : list50.sort((a, b) => { if (a.enqueueCountDiff < b.enqueueCountDiff) return -1 if (a.enqueueCountDiff > b.enqueueCountDiff) return 1 return 0 }); list188.sort((a, b) => { if (a.enqueueCountDiff < b.enqueueCountDiff) return -1 if (a.enqueueCountDiff > b.enqueueCountDiff) return 1 return 0 }); break; case 'enqueueDiffDescend' : list50.sort((a, b) => { if (a.enqueueCountDiff > b.enqueueCountDiff) return -1 if (a.enqueueCountDiff < b.enqueueCountDiff) return 1 return 0 }); list188.sort((a, b) => { if (a.enqueueCountDiff > b.enqueueCountDiff) return -1 if (a.enqueueCountDiff < b.enqueueCountDiff) return 1 return 0 }); break; case 'nameAscend' : list50.sort((a, b) => a.name.localeCompare(b.name)); list188.sort((a, b) => a.name.localeCompare(b.name)); break; case 'nameDescend' : list50.sort((a, b) => b.name.localeCompare(a.name)); list188.sort((a, b) => b.name.localeCompare(a.name)); break; } this.$set(this.queueNameList[this.server50], list50); this.$set(this.queueNameList[this.server188], list188); this.$forceUpdate(); // 强制更新组件 this.showQueueSort = false;帮我举个例子吧 写成代码
时间: 2024-03-16 17:45:00 浏览: 55
好的,这里是一个使用归并排序的例子,你可以尝试使用它替换掉你原来的排序算法,看看是否能够减少页面卡顿的情况。
```
function mergeSort(arr, compare) {
if (arr.length <= 1) {
return arr;
}
const mid = Math.floor(arr.length / 2);
const left = arr.slice(0, mid);
const right = arr.slice(mid);
return merge(mergeSort(left, compare), mergeSort(right, compare), compare);
}
function merge(left, right, compare) {
let i = 0;
let j = 0;
const result = [];
while (i < left.length && j < right.length) {
if (compare(left[i], right[j]) <= 0) {
result.push(left[i++]);
} else {
result.push(right[j++]);
}
}
return result.concat(i < left.length ? left.slice(i) : right.slice(j));
}
let list50 = this.queueNameList[this.server50];
let list188 = this.queueNameList[this.server188];
console.log(this.queueSort);
switch (this.queueSort) {
case "enqueueDiffAscend":
list50 = mergeSort(list50, (a, b) => {
if (a.enqueueCountDiff < b.enqueueCountDiff) return -1;
if (a.enqueueCountDiff > b.enqueueCountDiff) return 1;
return 0;
});
list188 = mergeSort(list188, (a, b) => {
if (a.enqueueCountDiff < b.enqueueCountDiff) return -1;
if (a.enqueueCountDiff > b.enqueueCountDiff) return 1;
return 0;
});
break;
case "enqueueDiffDescend":
list50 = mergeSort(list50, (a, b) => {
if (a.enqueueCountDiff > b.enqueueCountDiff) return -1;
if (a.enqueueCountDiff < b.enqueueCountDiff) return 1;
return 0;
});
list188 = mergeSort(list188, (a, b) => {
if (a.enqueueCountDiff > b.enqueueCountDiff) return -1;
if (a.enqueueCountDiff < b.enqueueCountDiff) return 1;
return 0;
});
break;
case "nameAscend":
list50 = mergeSort(list50, (a, b) => a.name.localeCompare(b.name));
list188 = mergeSort(list188, (a, b) => a.name.localeCompare(b.name));
break;
case "nameDescend":
list50 = mergeSort(list50, (a, b) => b.name.localeCompare(a.name));
list188 = mergeSort(list188, (a, b) => b.name.localeCompare(a.name));
break;
}
this.$set(this.queueNameList[this.server50], list50);
this.$set(this.queueNameList[this.server188], list188);
this.$forceUpdate(); // 强制更新组件
this.showQueueSort = false;
```
阅读全文