How to implement table Sorting According to it's any column by Jquery. I don't want any plugin. Just by pure jquery.
-
why you don't want to use any plugin? If you will use any plugin, your code size will be reduced and also you don't need to validate the whole code. So use "datatable.js" plugin to sort the tables.Azhar Mansuri– Azhar Mansuri2015-01-28 10:47:56 +00:00Commented Jan 28, 2015 at 10:47
-
First rule of coding: Don't Reinvent The Wheel!!!!Tiago Guedes– Tiago Guedes2015-01-28 10:56:32 +00:00Commented Jan 28, 2015 at 10:56
Add a comment
|
1 Answer
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.