Skip to content

Instantly share code, notes, and snippets.

@swapnilshrikhande
Created April 7, 2016 12:10
Show Gist options
  • Save swapnilshrikhande/4d0007c31cf9c8d047fc7e7d57d3283e to your computer and use it in GitHub Desktop.
Save swapnilshrikhande/4d0007c31cf9c8d047fc7e7d57d3283e to your computer and use it in GitHub Desktop.
Sort a table based on column containing inputs and input values.
var people, asc1 = 1,
asc2 = 1,
asc3 = 1;
function sort_table(col, asc) {
actualData = document.getElementById("actualData");
var rows = actualData.rows,
rlen = rows.length,
arr = new Array(),
arrshow= new Array(),
i, j, cells, clen;
for (i = 0; i < rlen; i++) {
cells = rows[i].cells;
clen = cells.length;
arr[i] = new Array();
for (j = 0; j < clen; j++) {
if (cells[j].className=="wrapperInput"){
arr[i][j]=cells[j].childNodes[0].value;
cells[j].childNodes[0].setAttribute('value',arr[i][j]);
}else{
arr[i][j] = cells[j].innerHTML;
}
}
arr[i].showData = rows[i].innerHTML;
}
// sort the array by the specified column number (col) and order (asc)
arr.sort(function (a, b) {
return (a[col] == b[col]) ? 0 : ((a[col] > b[col]) ? asc : -1 * asc);
});
for (i = 0; i < rlen; i++) {
rows[i].innerHTML = arr[i].showData;
}
}
<th class="header" onclick="sort_table( 1, asc1); asc1 *= -1; asc2 = 1; asc3 = 1;">
<td class="wrapperInput">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment