-
Notifications
You must be signed in to change notification settings - Fork 0
/
sortTable.js
57 lines (55 loc) · 1.32 KB
/
sortTable.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
export { sortTable };
function sortTable(n) {
let table,
rows,
switching,
i,
x,
y,
shouldSwitch,
dir,
switchcount = 0;
table = document.querySelector("#userTable");
switching = true;
dir = "asc";
while (switching) {
switching = false;
rows = table.querySelectorAll("tr");
for (i = 1; i < rows.length - 1; i++) {
shouldSwitch = false;
x = rows[i].querySelectorAll("td")[n];
y = rows[i + 1].querySelectorAll("td")[n];
let xContent = isNaN(x.innerHTML)
? x.innerHTML.toLowerCase() === "-"
? 0
: x.innerHTML.toLowerCase()
: parseFloat(x.innerHTML);
let yContent = isNaN(y.innerHTML)
? y.innerHTML.toLowerCase() === "-"
? 0
: y.innerHTML.toLowerCase()
: parseFloat(y.innerHTML);
if (dir == "asc") {
if (xContent > yContent) {
shouldSwitch = true;
break;
}
} else if (dir == "desc") {
if (xContent < yContent) {
shouldSwitch = true;
break;
}
}
}
if (shouldSwitch) {
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
switching = true;
switchcount++;
} else {
if (switchcount == 0 && dir == "asc") {
dir = "desc";
switching = true;
}
}
}
}