This question might seem easy for some, but I am new to JavaScript.
I'm having trouble understanding this part of the function:
kids.sort(function(n, m)
What does n and m represent? How can I grasp these types of functions?
Thanks!
<script>
function sortkids(e) {
if (typeof e == "string") e = document.getElementById(e);
var kids = [];
for(var x = e.firstChild; x != null; x = x.nextSibling)
if (x.nodeType == 1) kids.push(x);
kids.sort(function(n, m) {
var s = n.firstChild.data;
var t = m.firstChild.data;
if (s < t) return -1;
else if (s > t) return 1;
else return 0;
});
for(var i = 0; i < kids.length; i++) e.appendChild(kids[i]);
}
</script>
<ul id="list">
<li>one<li>two<li>three<li>four <!-- items are not in alphabetical order -->
</ul>
<button onclick="sortkids('list')">Sort list</button>