1/*--------------------------------------------------------------*/ 2// HTML TABLE SORTER 3// OBJECT ORIENTED JAVASCRIPT IMPLEMENTATION OF QUICKSORT 4// @author Terrill Dent 5// @source http://www.terrill.ca 6// @date August 28th, 2006 7/*--------------------------------------------------------------*/ 8function TSorter(){ 9 var table = Object; 10 var trs = Array; 11 var ths = Array; 12 var curSortCol = Object; 13 var prevSortCol = '3'; 14 var sortType = Object; 15 16 function get(){} 17 18 function getCell(index){ 19 return trs[index].cells[curSortCol] 20 } 21 22 /*----------------------INIT------------------------------------*/ 23 // Initialize the variable 24 // @param tableName - the name of the table to be sorted 25 /*--------------------------------------------------------------*/ 26 this.init = function(tableName) 27 { 28 table = document.getElementById(tableName); 29 ths = table.getElementsByTagName("th"); 30 for(var i = 0; i < ths.length ; i++) 31 { 32 ths[i].onclick = function() 33 { 34 sort(this); 35 } 36 } 37 return true; 38 }; 39 40 /*----------------------SORT------------------------------------*/ 41 // Sorts a particular column. If it has been sorted then call reverse 42 // if not, then use quicksort to get it sorted. 43 // Sets the arrow direction in the headers. 44 // @param oTH - the table header cell (<th>) object that is clicked 45 /*--------------------------------------------------------------*/ 46 function sort(oTH) 47 { 48 curSortCol = oTH.cellIndex; 49 sortType = oTH.abbr; 50 trs = table.tBodies[0].getElementsByTagName("tr"); 51 52 //set the get function 53 setGet(sortType) 54 55 // it would be nice to remove this to save time, 56 // but we need to close any rows that have been expanded 57 for(var j=0; j<trs.length; j++) 58 { 59 if(trs[j].className == 'detail_row') 60 { 61 closeDetails(j+2); 62 } 63 } 64 65 // if already sorted just reverse 66 if(prevSortCol == curSortCol) 67 { 68 oTH.className = (oTH.className != 'ascend' ? 'ascend' : 'descend' ); 69 reverseTable(); 70 } 71 // not sorted - call quicksort 72 else 73 { 74 oTH.className = 'ascend'; 75 if(ths[prevSortCol].className != 'exc_cell'){ths[prevSortCol].className = '';} 76 quicksort(0, trs.length); 77 } 78 prevSortCol = curSortCol; 79 } 80 81 /*--------------------------------------------------------------*/ 82 // Sets the GET function so that it doesnt need to be 83 // decided on each call to get() a value. 84 // @param: colNum - the column number to be sorted 85 /*--------------------------------------------------------------*/ 86 function setGet(sortType) 87 { 88 switch(sortType) 89 { 90 case "link_column": 91 get = function(index){ 92 return getCell(index).firstChild.firstChild.nodeValue; 93 }; 94 break; 95 default: 96 get = function(index){ return getCell(index).firstChild.nodeValue;}; 97 break; 98 }; 99 } 100 101 /*-----------------------EXCHANGE-------------------------------*/ 102 // A complicated way of exchanging two rows in a table. 103 // Exchanges rows at index i and j 104 /*--------------------------------------------------------------*/ 105 function exchange(i, j) 106 { 107 if(i == j+1) { 108 table.tBodies[0].insertBefore(trs[i], trs[j]); 109 } else if(j == i+1) { 110 table.tBodies[0].insertBefore(trs[j], trs[i]); 111 } else { 112 var tmpNode = table.tBodies[0].replaceChild(trs[i], trs[j]); 113 if(typeof(trs[i]) == "undefined") { 114 table.appendChild(tmpNode); 115 } else { 116 table.tBodies[0].insertBefore(tmpNode, trs[i]); 117 } 118 } 119 } 120 121 /*----------------------REVERSE TABLE----------------------------*/ 122 // Reverses a table ordering 123 /*--------------------------------------------------------------*/ 124 function reverseTable() 125 { 126 for(var i = 1; i<trs.length; i++) 127 { 128 table.tBodies[0].insertBefore(trs[i], trs[0]); 129 } 130 } 131 132 /*----------------------QUICKSORT-------------------------------*/ 133 // This quicksort implementation is a modified version of this tutorial: 134 // http://www.the-art-of-web.com/javascript/quicksort/ 135 // @param: lo - the low index of the array to sort 136 // @param: hi - the high index of the array to sort 137 /*--------------------------------------------------------------*/ 138 function quicksort(lo, hi) 139 { 140 if(hi <= lo+1) return; 141 142 if((hi - lo) == 2) { 143 if(get(hi-1) > get(lo)) exchange(hi-1, lo); 144 return; 145 } 146 147 var i = lo + 1; 148 var j = hi - 1; 149 150 if(get(lo) > get(i)) exchange(i, lo); 151 if(get(j) > get(lo)) exchange(lo, j); 152 if(get(lo) > get(i)) exchange(i, lo); 153 154 var pivot = get(lo); 155 156 while(true) { 157 j--; 158 while(pivot > get(j)) j--; 159 i++; 160 while(get(i) > pivot) i++; 161 if(j <= i) break;
162 exchange(i, j); 163 } 164 exchange(lo, j); 165 166 if((j-lo) < (hi-j)) { 167 quicksort(lo, j); 168 quicksort(j+1, hi); 169 } else { 170 quicksort(j+1, hi); 171 quicksort(lo, j); 172 } 173 } 174} 175function setGet(sortType) 176{ 177 switch(sortType) 178 { 179 case "link_column": 180 get = function(index) 181 { 182 return getCell(index).firstChild.firstChild.nodeValue; 183 }; 184 break; 185 default: 186 get = function(index){ return getCell(index).firstChild.nodeValue;}; 187 break; 188 }; 189}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.