Improve performance of sorting

This commit is contained in:
Dennis Eichhorn 2018-09-30 19:18:23 +02:00
parent b9761f16bb
commit 9e5f78c3ea

View File

@ -132,11 +132,12 @@
const table = document.getElementById(id), const table = document.getElementById(id),
rows = table.getElementsByTagName('tbody')[0].rows, rows = table.getElementsByTagName('tbody')[0].rows,
rowLength = rows.length, rowLength = rows.length,
rowId = this.closest('tr').rowIndex; rowId = this.closest('tr').rowIndex,
orderType = jsOMS.hasClass(this, 'order-up') ? 1 : -1;
if (jsOMS.hasClass(this, 'order-up') && rowId > 1) { if (orderType === 1 && rowId > 1) {
rows[rowId].parentNode.insertBefore(rows[rowId - 2], rows[rowId]); rows[rowId].parentNode.insertBefore(rows[rowId - 2], rows[rowId]);
} else if (jsOMS.hasClass(this, 'order-down') && rowId < rowLength) { } else if (orderType === -1 && rowId < rowLength) {
rows[rowId - 1].parentNode.insertBefore(rows[rowId], rows[rowId - 1]); rows[rowId - 1].parentNode.insertBefore(rows[rowId], rows[rowId - 1]);
} }
}); });
@ -151,9 +152,10 @@
const table = document.getElementById(id), const table = document.getElementById(id),
rows = table.getElementsByTagName('tbody')[0].rows, rows = table.getElementsByTagName('tbody')[0].rows,
rowLength = rows.length, rowLength = rows.length,
cellId = this.closest('td').cellIndex; cellId = this.closest('td').cellIndex,
sortType = jsOMS.hasClass(this, 'sort-asc') ? 1 : -1;
let j, row1, row2, content1, content2, let j, i, row1, row2, content1, content2,
order = false, order = false,
shouldSwitch = false; shouldSwitch = false;
@ -163,21 +165,28 @@
for (j = 0; j < rowLength - 1; ++j) { for (j = 0; j < rowLength - 1; ++j) {
shouldSwitch = false; shouldSwitch = false;
row1 = rows[j].getElementsByTagName('td')[cellId]; row1 = rows[j].getElementsByTagName('td')[cellId];
row2 = rows[j + 1].getElementsByTagName('td')[cellId];
content1 = row1.getAttribute('data-content') !== null ? row1.getAttribute('data-content') : row1.textContent; content1 = row1.getAttribute('data-content') !== null ? row1.getAttribute('data-content') : row1.textContent;
content2 = row2.getAttribute('data-content') !== null ? row2.getAttribute('data-content') : row2.textContent;
if (jsOMS.hasClass(this, 'sort-asc') && content1 > content2) { for (i = j + 1; i < rowLength; ++i) {
shouldSwitch = true; row2 = rows[i].getElementsByTagName('td')[cellId];
break; content2 = row2.getAttribute('data-content') !== null ? row2.getAttribute('data-content') : row2.textContent;
} else if (jsOMS.hasClass(this, 'sort-desc') && content1 < content2) {
shouldSwitch = true; if (sortType === 1 && content1 > content2) {
shouldSwitch = true;
} else if (sortType === -1 && content1 < content2) {
shouldSwitch = true;
} else {
break;
}
}
if (shouldSwitch === true) {
break; break;
} }
} }
if (shouldSwitch) { if (shouldSwitch) {
rows[j].parentNode.insertBefore(rows[j + 1], rows[j]); rows[j].parentNode.insertBefore(rows[i - 1], rows[j]);
order = true; order = true;
} }
} while (order); } while (order);