// functions to sort columns on the sexoffenders web page
var asc1 = 1, asc2 = 1, asc3 = 1, asc4 = 1, asc5 = 1;
function sort_table(col, asc) {
var rows = document.getElementById("offendersTable").rows,
rlen = rows.length,
arr = new Array(), i, j, cells, clen;
// fill the array with values from the table
for (i = 1; i < rlen; i++) {
cells = rows[i].cells;
clen = cells.length;
arr[i - 1] = new Array();
for (j = 0; j < clen; j++) {
arr[i - 1][j] = cells[j].innerHTML;
}
}
// sort the array by the specified column number (col) and order (asc)
arr.sort(function (a, b) {
var val1 = a[col].replace(/<(?:.|\n)*?>/gm, '');
var val2 = b[col].replace(/<(?:.|\n)*?>/gm, '')
if (col == 4) { // date column
var parts = val1.split('/');
var yr = parts[2];
if (parseInt(yr) > 60) {
yr = '19' + yr;
} else {
yr = '20' + yr;
}
var mo = parts[0].length < 2 ? '0' + parts[0] : parts[0];
var da = parts[1].length < 2 ? '0' + parts[1] : parts[1];
val1 = yr + mo + da;
parts = val2.split('/');
yr = parts[2];
if (parseInt(yr) > 60) {
yr = '19' + yr;
} else {
yr = '20' + yr;
}
mo = parts[0].length < 2 ? '0' + parts[0] : parts[0];
da = parts[1].length < 2 ? '0' + parts[1] : parts[1];
val2 = yr + mo + da;
}
return (val1 == val2) ? 0 : ((val1 > val2) ? asc : -1 * asc);
});
// replace existing rows with new rows created from the sorted array
for (i = 1; i < rlen; i++) {
rows[i].innerHTML = "";
for (j = 0; j < clen; j++) {
try
{
rows[i].innerHTML += "
" + arr[i - 1][j] + " | ";
}
catch(err)
{
alert(err);
}
}
}
}
// this is a bubble sort - not using it due to it being too slow
function sortTable(sortType) {
var table, rows, switching, i, x, y, shouldSwitch, sortColumn, val1, val2;
sortColumn = 0;
if (sortType == 'type') {
sortColumn = 0;
} else if (sortType == 'name') {
sortColumn = 1;
} else if (sortType == 'address') {
sortColumn = 2;
} else if (sortType == 'city') {
sortColumn = 3;
} else if (sortType == 'updated') {
sortColumn = 4;
}
table = document.getElementById("offendersTable");
switching = true;
/*Make a loop that will continue until
no switching has been done:*/
while (switching) {
//start by saying: no switching is done:
switching = false;
rows = table.rows;
/*Loop through all table rows (except the
first, which contains table headers):*/
for (i = 1; i < (rows.length - 1) ; i++) {
//start by saying there should be no switching:
shouldSwitch = false;
/*Get the two elements you want to compare,
one from current row and one from the next:*/
x = rows[i].getElementsByTagName("TD")[sortColumn];
y = rows[i + 1].getElementsByTagName("TD")[sortColumn];
if (sortColumn != 4) {
val1 = x.innerText.toLowerCase();
val2 = y.innerText.toLowerCase();
} else { // date in reverse order
var parts = x.innerText.split('/');
var yr = parts[2];
if (parseInt(yr) > 60) {
yr = '19' + yr;
} else {
yr = '20' + yr;
}
var mo = parts[0].length < 2 ? '0' + parts[0] : parts[0];
var da = parts[1].length < 2 ? '0' + parts[1] : parts[1];
val2 = yr + mo + da;
parts = y.innerText.split('/');
yr = parts[2];
if (parseInt(yr) > 60) {
yr = '19' + yr;
} else {
yr = '20' + yr;
}
mo = parts[0].length < 2 ? '0' + parts[0] : parts[0];
da = parts[1].length < 2 ? '0' + parts[1] : parts[1];
val1 = yr + mo + da;
}
//check if the two rows should switch place:
if (val1 > val2) {
//if so, mark as a switch and break the loop:
shouldSwitch = true;
break;
}
}
if (shouldSwitch) {
/*If a switch has been marked, make the switch
and mark that a switch has been done:*/
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
switching = true;
}
}
}