Reputation: 2664
How to implement table Sorting According to it's any column by Jquery. I don't want any plugin. Just by pure jquery.
Upvotes: 0
Views: 826
Reputation: 2664
We can use jquery.
var $tbody = $('table tbody');
$tbody.find('tr').sort(function (a, b) {
var tda = $(a).find('td:eq(' + ColumnIndex + ')').text(); // Use your wished column index
var tdb = $(b).find('td:eq(' + ColumnIndex + ')').text(); // Use your wished column index
// if a < b return 1
return tda > tdb ? 1
// else if a > b return -1
: tda < tdb ? -1
// else they are equal - return 0
: 0;
}).appendTo($tbody);
Use < instead of >for descending.
Upvotes: 3