I need to sort an alphaNumeric list. I am able to sort but some how because it is alphaNumeric, I am not able to put the abc1 before the abc10.
My HTML Code:
<ul class="theList">
<li><b>abc11:</b>Hello</li>
<li><b>abc10:</b>Hello</li>
<li><b>xyz24:</b>Hello</li>
<li><b>abc1:</b>Hello</li>
<li><b>xyz2:</b>Hello</li>
</ul>
My JavaScript:
$(document).ready(function() {
var list, i, switching, b, shouldSwitch;
list = document.getElementsByClassName("theList");
for (var j = 0; j < list.length; j++) {
switching = true;
while (switching) {
switching = false;
b = list[j].getElementsByTagName("li");
for (i = 0; i < (b.length - 1); i++) {
shouldSwitch = false;
if (b[i].innerHTML.toLowerCase() > b[i + 1].innerHTML.toLowerCase()) {
shouldSwitch = true;
break;
}
}
if (shouldSwitch) {
b[i].parentNode.insertBefore(b[i + 1], b[i]);
switching = true;
}
}
}
});
My Result:
- abc10:Hello
- abc11:Hello
- abc1:Hello
- xyz24:Hello
- xyz2:Hello
Expected Result:
- abc1:Hello
- abc10:Hello
- abc11:Hello
- xyz2:Hello
- xyz24:Hello
What am I missing?