@ZeroKilledMar 01.2009 — #some time ago i wanted to achieve something similar: sort in alphanumerical the child of a node within a given element, but suddenly I give up. what follow is the code i came up today. i don't like the code, but anyway, maybe you can grasp some idea: [code=html] <div id='tosort'> <p>one phrase</p> <p>the next phrase</p> <p>z last because begin with z</p> <p>and first</p> <div>contain string'</div> </div>
<script type='text/javascript'> onload = function(){ var nodes = []; var text = []; var elem = document.getElementById('tosort');
for(var i = 0; i < elem.childNodes.length; i++)if(elem.childNodes[i].nodeType == 1)nodes.push(elem.childNodes[i]); for(var i = 0; i < nodes.length; i++)text.push(nodes[i].firstChild.nodeValue); text.sort(); while(text.length){ for(var j = 0; j < nodes.length; j++)if(text[0] == nodes[j].firstChild.nodeValue)elem.appendChild(nodes[j]); text.shift(); } } </script> [/code]
[B][SIZE="3"]about the code[/SIZE][/B]
the code operate correctly if each child only contain text. that is, if the firrst child of each nodes is an element, then the code can't grab the text. guess this is the main reason i don't like the code.
@cluettrauthorMar 03.2009 — #Thanks, what I did was keep multiple arrays and then processed a function against those arrays to determine where new items should be inserted. It works for the most part although I'm having an issue with the splice array method. I'm going to post about that shortly. Very odd behavior.