-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort.js
86 lines (77 loc) · 3.32 KB
/
sort.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
function sortTable(method) {
var table, rows, x, y, shouldSwitch, switching, asc;
table = document.getElementById("fav-table");
switching = true;
asc = ($('#select-order').val() === 'Ascending');
console.log("ascending is " + asc);
while(switching) {
switching = false;
rows = table.getElementsByTagName("tr");
for(var i = 1,len = rows.length - 1; i < len; i++ ) {
shouldSwitch = false;
switch (method) {
case "Symbol":
// console.log($('td', rows[i]));
x = $('td', rows[i])[0].firstChild.innerHTML;
y = $('td', rows[i + 1])[0].firstChild.innerHTML;
// console.log("x = " + x);
// console.log("y = " + y);
break;
case "Price":
x = parseFloat($('td', rows[i])[1].innerHTML);
y = parseFloat($('td', rows[i + 1])[1].innerHTML);
break;
case "Change":
x = parseFloat($('td', rows[i])[2].innerHTML.split('(')[0]);
y = parseFloat($('td', rows[i + 1])[2].innerHTML.split('(')[0]);
break;
case "Change Percent":
x = parseFloat($('td', rows[i])[2].innerHTML.split('%')[0].split('(')[1]);
y = parseFloat($('td', rows[i+1])[2].innerHTML.split('%')[0].split('(')[1]);
break;
case "Volume":
x = parseInt($('td', rows[i])[3].innerHTML.replace(/,/g , ''));
y = parseInt($('td', rows[i + 1])[3].innerHTML.replace(/,/g , ''));
break;
default:
x = JSON.parse(localStorage.getItem($('td', rows[i])[0].firstChild.innerHTML))["defaultOrder"];
y = JSON.parse(localStorage.getItem($('td', rows[i+1])[0].firstChild.innerHTML))["defaultOrder"];
asc = true;
break;
}
if (asc) {
if (x > y) {
shouldSwitch = true;
break;
}
} else {
if (x < y) {
shouldSwitch = true;
break;
}
}
}
if(shouldSwitch) {
// console.log(rows[i].parentNode);
rows[i].parentNode.insertBefore(rows[i+1], rows[i]);
// console.log(table);
switching = true;
}
}
}
$('#select-method').change(function () {
var method = $(this).val();
if(method === 'Default') {
$('#select-order').prop('disabled', true);
sortTable('default');
}
else {
$('#select-order').prop('disabled', false);
sortTable(method);
}
$('#select-order').selectpicker('refresh');
});
$('#select-order').change(function () {
var method = $('#select-method').val();
sortTable(method);
});