I\'m trying to sort an li elements and get an unexpacted result
I need to sort it three times to get it correctly,
where have I mistaken? javascript
There are much better ways to sort.
localeCompare() is such a comparison function. "#table1" is a more efficient selector than "ol#table1".I would suggest this:
$("div#btn").click(function() {
var sort_by_name = function(a, b) {
return a.innerHTML.toLowerCase().localeCompare(b.innerHTML.toLowerCase());
}
var list = $("#table1 > li").get();
list.sort(sort_by_name);
for (var i = 0; i < list.length; i++) {
list[i].parentNode.appendChild(list[i]);
}
});
Which you can see work here: http://jsfiddle.net/jfriend00/yqd3w/