Simple sorting by script number, 3 lines, doesn't sort the last few words correctly, why?

http://jsfiddle.net/nicktheandroid/6BAfH/1/

List items are sorted accordingly by a number in their range. Why are the latest numbers 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> 
+7
source share
1 answer

Welcome to the world of octal numbers.

If the input line starts with "0", the radius is eight (octal). This function is non-standard, and some implementations intentionally do not support it (use radix 10 instead). For this reason, always specify the radius when using parseInt.

Use base with base 10 with parseInt .

 parseInt($('span', a).text(), 10) 
+13
source

All Articles