Skip to content Skip to sidebar Skip to footer

Sort Table Rows According To Table Data

For example, I have a code:
nameprice
a<

Solution 1:

Instead of

return 1 * $(a).find('.sort').text() < 1 * $(b).find('.sort').text() ? 1 : 0;

insert

return 1 * $(a).find('.sort').text() < 1 * $(b).find('.price').text() ? 0 : 1;

http://jsfiddle.net/E56j8/

Solution 2:

You have number of plugins to sort it why are you reinventing the wheel. Here is one such plugin

Link

<scripttype="text/javascript"src="/path/to/jquery-latest.js"></script><scripttype="text/javascript"src="/path/to/jquery.tablesorter.js"></script> 

$("#myTable").tablesorter(); 

Solution 3:

Have a look at Sorting - we're doing it wrong. A simple jQuery plugin for sorting stuff is available here.


some notes on your code:

// you're binding a document ready event within a function call? // looks the wrong way 'round, to mefunctionsortTheTable(){
    $(function() {
        // 1) you probably want to use .detach() over .remove()// 2) "tr:has(.price)" will match ALL table rows // containing an element with the class .price// even if they're children of different <table>s!// 3) $('.selector') is already "an array", at least it's sortable right away.// there's no need for $.makeArray() herevar elems = $.makeArray($('tr:has(.price)').remove())
        elems.sort(sortNum)
        // "#information" is a sufficient (and more efficient) selector, 
        $('table#information').append($(elems));
    });
}

Post a Comment for "Sort Table Rows According To Table Data"