Skip to content Skip to sidebar Skip to footer

Jquery Sortelement: Sort By Numerical Value Of Table Data

Here's the jsFiddle. As you can see from the fiddle, the total column contains numbers that are currently sorted by text. Q: How can I sort the total column by the numerical value

Solution 1:

You have to parse the values before you compare them.

.sortElements(function(a, b){
    var sa = $.text([a]);
    var sb = $.text([b]);

    var ia = parseInt(sa);
    var ib = parseInt(sb);

    if (!isNaN(ia) && !isNaN(ib)) {
        return ia > ib ? (inverse ? -1 : 1) : (inverse ? 1 : -1);
    }

    return sa > sb ? (inverse ? -1 : 1) : (inverse ? 1 : -1);
}

Here's your updated jsfiddle.

Post a Comment for "Jquery Sortelement: Sort By Numerical Value Of Table Data"