Is it possible to sort the list bellow from the lower number to the bigger number, keeping each li content?
-
39
<
Your function should be like this.
function numOrdDesc(a, b) {
var aTxt = parseInt($(a)[0].innerText, 10);
var bTxt = parseInt($(b)[0].innerText, 10);
return (bTxt - aTxt);
}
DEMO
Try below code,
$(function() {
$('button').click(function() {
var liContents = [];
$('ul li').each(function() {
liContents.push($(this).html());
});
liContents.sort(liSorter);
$('ul li').each(function() {
$(this).html(liContents.pop());
});
});
});
/*
Below function is kind of a workaround for the listed HTMl
you need to update it if you have proper HTML.
*/
function liSorter(a, b) {
return (parseInt(b) - parseInt(a));
}
DEMO
Edit: Updated your markup a little for a better code,
span
tags like <span class="num">39</span>
Code:
function numOrdDesc(a, b) {
var aTxt = parseInt($(a).find('.num').text(), 10);
var bTxt = parseInt($(b).find('.num').text(), 10);
return (bTxt - aTxt);
}
DEMO