Convert the NodeList
to an array first:
var elements = [].slice.call(table.tBodies[0].childNodes);
and then call sort
normally:
elements.sort(down);
It seems sort
cannot handle array-like objects. This is probably because NodeList
does not provide any methods to change the list, but sort
sorts the array in-place.
Update: For more information, from the specification:
Perform an implementation-dependent sequence of calls to the [[Get]] , [[Put]], and [[Delete]] internal methods of obj.
I assume NodeList
s don't have these internal methods. But this is really just an assumption. It could also be that this is implementation dependent.
I also suggest you use .children
[MDN] instead of .childNodes
to only get element nodes. Update: Or .rows
[DOM Spec] as @patrick suggests.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…