It's considered incorrect in HTML4 to start IDs with numbers. This rarely causes problems in browsers, but it's also easily avoided.
I replaced your IDs with data-
attributes, which are automatically extracted by the .data()
method in jQuery and converted into numbers, eliminating the need for parseInt
.
HTML:
<ul id="cars">
<li data-val="2">Ford</li>
<li data-val="1">Volvo</li>
<li data-val="3">Fiat</li>
</ul>
JS:
$('#cars').children('li').sort(function(a, b) {
return $(a).data('val')-$(b).data('val');
}).appendTo('#cars');
Fiddle: http://jsfiddle.net/qaytJ/1/
data-
attributes are useful whenever you want to attach arbitrary data to HTML elements, and it's more "correct", or at least more appropriate, than forcing id
or class
to do a job it wasn't intended to do. Use them often.