http://jsfiddle.net/nicktheandroid/6BAfH/1/
The list-elements are sorted accordingly by the number in their span. Why is it that the last few numbers are out of order? I'm confused.
Jquery
function sortEm(a,b){
return parseInt($('span', a).text()) < parseInt($('span', b).text()) ? 1 : -1;
}
$('li').sort(sortEm).prependTo($('ul#test'));
HTML
<ul id="test">
<li> Cups
<span>12</span>
</li>
<li> Plates
<span>18</span>
</li>
<li> Forks
<span>03</span>
</li>
<li> Knives
<span>08</span>
</li>
<li> Bowls
<span>55</span>
</li>
</ul>
Welcome to the world of octal numbers.
If the input string begins with "0", radix is eight (octal). This feature is non-standard, and some implementations deliberately do not support it (instead using the radix 10). For this reason always specify a radix when using parseInt.
Use the radix to base 10 with parseInt.
parseInt($('span', a).text(), 10)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With