Skip to content

Commit 5be4b46

Browse files
fix: Sort button now sorts both strings and numbers
Co-authored-by: Ana Rocha <[email protected]>
1 parent 4a5ee15 commit 5be4b46

File tree

1 file changed

+29
-24
lines changed

1 file changed

+29
-24
lines changed

src/index.js

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -105,41 +105,46 @@ document.addEventListener("DOMContentLoaded", function() {
105105

106106
function addSortButton(el, idx) {
107107
const button = document.createElement('button');
108-
if (idx === 0 || idx === 5 || idx === 6) {
109-
button.innerText = 'Sort';
110-
button.addEventListener("click", function() {
111-
sortTable(idx);
112-
});
113-
button.setAttribute('style', 'justify-content: center');
114-
}
108+
const img = document.createElement('img');
109+
button.innerText = 'Sort';
110+
button.addEventListener("click", function() {
111+
sortTable(idx);
112+
});
113+
img.addEventListener("click", function() {
114+
sortTable(idx);
115+
});
116+
button.setAttribute('style', 'justify-content: center');
117+
115118
el.appendChild(button);
116119
}
117120

118121
let direction = false;
119122
function sortTable(columnIndex) {
120123
removeAllChildNodes(tbody);
121-
if (columnIndex === 0) {
124+
if (isNaN(rows[0].columns[columnIndex][0]) && isNaN(rows[rows.length - 1].columns[columnIndex][0])) {
122125
rows.sort((a, b) => {
123-
a = a.columns[columnIndex].split("-");
124-
b = b.columns[columnIndex].split("-");
125-
if (a[0] === b[0]) {
126-
return direction ? a[1] - b[1] : b[1] - a[1];
127-
} else {
128-
return direction ? a[0] - b[0] : b[0] - a[0];
129-
}
126+
a = a.columns[columnIndex];
127+
b = b.columns[columnIndex];
128+
return direction ? a.localeCompare(b) : b.localeCompare(a);
130129
});
131-
132-
for (let i = 0; i <= rows.length - 1; i++) {
133-
tbody.appendChild(rows[i].el);
134-
}
135130
} else {
136-
rows.sort((a, b) => {
137-
return direction ? a.columns[columnIndex] - b.columns[columnIndex] : b.columns[columnIndex] - a.columns[columnIndex];
138-
});
139-
for (let i = 0; i <= rows.length - 1; i++) {
140-
tbody.appendChild(rows[i].el);
131+
if (!columnIndex && window.location.href.includes("docs/alerts")) {
132+
rows.sort((a, b) => {
133+
a = a.columns[columnIndex].split("-");
134+
b = b.columns[columnIndex].split("-");
135+
return direction ? a[0] - b[0] : b[0] - a[0];
136+
});
137+
} else {
138+
rows.sort((a, b) => {
139+
a = a.columns[columnIndex];
140+
b = b.columns[columnIndex];
141+
return direction ? a - b : b - a;
142+
});
141143
}
142144
}
145+
for (let i = 0; i <= rows.length - 1; i++) {
146+
tbody.appendChild(rows[i].el);
147+
}
143148
direction = !direction;
144149
}
145150

0 commit comments

Comments
 (0)