Skip to content

Instantly share code, notes, and snippets.

@Millsky
Last active April 1, 2017 16:48
Show Gist options
  • Save Millsky/db4a1255f315acf87ecdc3b6c243bf05 to your computer and use it in GitHub Desktop.
Save Millsky/db4a1255f315acf87ecdc3b6c243bf05 to your computer and use it in GitHub Desktop.
function sortTable(table,col){
if(table.nodeName != "TABLE"){
throw new Error("Node is not of type TABLE");
return;
}
/* DEEP CLONE WILL BREAK THE REF */
//table = table.cloneNode(true);
var rows = [].slice.call(table.tBodies[0].rows,1);
var tBody = table.tBodies[0];
/* SORT MAP AND REPLACE */
table.tBodies[0].rows = rows.sort(function (a,b) {
return a.cells[col].innerText > b.cells[col].innerText;
}).map(function (row) {
/* SINCE WE HAVE A REF TO THE PARENT THEN WE CAN SIMPLY CALL APPEND CHILD */
tBody.appendChild(row);
});
return table;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment