Last active
April 1, 2017 16:48
-
-
Save Millsky/db4a1255f315acf87ecdc3b6c243bf05 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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