-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolumnsort.js
112 lines (103 loc) · 2.92 KB
/
columnsort.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
* Custom column sorting application by Luther Tychonievich, 2013-04-15
* Treats numbers a single entities; this a22b33c < a22b100c
*
* USAGE: sortcolumn( "id-of-tbody-element", 3 ) to sort on the 4th column
* Assumes the existence of a header row
*/
function compareColumn( column ) {
return function(a,b) {
a = a.cells[column].innerHTML.replace(/<[^>]*>/g, "");
b = b.cells[column].innerHTML.replace(/<[^>]*>/g, "");
var i=0;
var j=0;
while( i < a.length && j < b.length ) {
var ac = a.charCodeAt(i);
i += 1;
var bc = b.charCodeAt(j);
j += 1;
if (ac >= 48 && ac <= 57 && bc >= 48 && bc <= 57) {
ac -= 48;
while (i < a.length && a.charCodeAt(i) >= 48 && a.charCodeAt(i) <= 57) {
ac = ac*10 + a.charCodeAt(i) - 48;
i += 1;
}
bc -= 48;
while (j < b.length && b.charCodeAt(j) >= 48 && b.charCodeAt(j) <= 57) {
bc = bc*10 + b.charCodeAt(j) - 48;
j += 1;
}
}
if (ac != bc) return ac - bc;
}
return (a.length - i) - (b.length - j);
}
}
function stableSort( array, func, si, ei ) {
if ( si === undefined ) { si = 0; ei = array.length-1; }
if ( si >= ei ) return;
var mi = Math.floor((si+ei)/2);
stableSort( array, func, si, mi );
stableSort( array, func, mi+1, ei );
var front = array.slice(si,mi+1);
var back = array.slice(mi+1,ei+1);
var fi = 0;
var bi = 0;
for (var ai = si; ai <= ei; ai += 1) {
if ( bi >= back.length || (fi < front.length && (func(front[fi], back[bi]) <= 0)) ) {
array[ai] = front[fi];
fi += 1;
} else {
array[ai] = back[bi];
bi += 1;
}
}
}
function sortcolumn( tbody, column, doalt ) {
var tab = document.getElementById( tbody );
var toSort = new Array();
for (var i=1; i<tab.rows.length; i+=1) {
toSort[i-1] = tab.rows[i];
}
var header = tab.rows[0];
// toSort.sort( compareColumn(column) );
stableSort( toSort, compareColumn(column) );
var changed = false;
for (var i=1; i<tab.rows.length; i+=1) {
if (toSort[i-1] != tab.rows[i]) changed = true;
}
if (!changed) toSort.reverse();
while (tab.hasChildNodes()) tab.removeChild( tab.lastChild );
tab.appendChild(header);
for (var i=0; i<toSort.length; i+=1) {
tab.appendChild(toSort[i]);
if (doalt) {
if ((i % 2) == 0) {
tab.lastChild.setAttribute("class","");
} else {
tab.lastChild.setAttribute("class","alt");
}
}
}
}
var filterrows = function(tbody, searchstring) {
var tab = document.getElementById( tbody );
var visCount = 0;
for (var i=1; i<tab.rows.length; i+=1) {
var contents = tab.rows[i].innerHTML;
var tag = /<[^><]*>/g;
while (contents.search(tag) >= 0) contents = contents.replace(tag,"\t");
contents = contents.replace(/\t\t+/,"\t");
var where = contents.toLowerCase().search(searchstring);
if ( where < 0 ) {
tab.rows[i].setAttribute("class","hide");
} else {
visCount += 1;
if ( (visCount%2) == 1 ) {
tab.rows[i].setAttribute("class","alt");
} else {
tab.rows[i].setAttribute("class","");
}
}
}
}