JavaScript Sorting Issue With Looping
I'm not entirely sure what my issue is here. I have the following sorting procedure that sorts a 'table' (Not an actual HTML table): function sortByName(tableBody) { var projec
Solution 1:
Assuming that the sortByName function-argument tableBody
is expecting a DOM Row collection of a certain table, - use this.:
function sortByName( tableBody ) {
var i = 0, projectList = [].slice.call(tableBody).
sort( function( a, b ){
var A = a.innerText.toUpperCase(), B = b.innerText.toUpperCase();
return A < B ? -1 : A > B ? 1 : 0;
}), wrapper = tableBody[0].parentElement;
while( projectList[i] )wrapper.appendChild( projectList[i++] );
}
I'm also assuming that with this, instant sort function you will not need to switch the display style property of elements, as they will be placed in their respective sort order, instantly!
Regards.
p.s.: still not sure what tableBody really is, therefore depending on the update to this info, the function given, might, or might not need further tweaking.
Solution 2:
You made an array out of elements from tableBody
(whatever that is), and then re-arrange those elements within that array. But you made no changes to tableBody
itself, which, even if you had, might not have the desired result (again, no idea what is in tableBody
).
Post a Comment for "JavaScript Sorting Issue With Looping"