Please see my comments to the question.
First of all, consider the simplest alternative: if you always need the same order, recent to last, just don't use
appendChild
but insert it always in the first position. Instead, you can use the property
firstChild
with the method
insertBefore
:
http://www.w3schools.com/dom/prop_element_firstchild.asp[
^],
http://www.w3schools.com/dom/met_element_insertbefore.asp[
^].
On each step, find a first child and then add a new element before it.
As usually, it's done much easier with jQuery:
http://api.jquery.com/prepend/[
^].
If you need to resort something dynamically, first consider my first comment to the question. In the meanwhile, you can get a lot of ideas on sorting:
http://www.tripwiremagazine.com/2012/05/jquery-filter-sort-plugins.html[
^].
If you need to learn jQuery (highly recommended), please see:
http://en.wikipedia.org/wiki/JQuery[
^],
http://jquery.com/[
^],
http://learn.jquery.com/[
^],
http://learn.jquery.com/using-jquery-core/[
^],
http://learn.jquery.com/about-jquery/how-jquery-works/[
^] (start from here).
—SA