0

How to implement table Sorting According to it's any column by Jquery. I don't want any plugin. Just by pure jquery.

SharmaPattar
  • 2,472
  • 3
  • 21
  • 23
  • 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 Jan 28 '15 at 10:47
  • First rule of coding: Don't Reinvent The Wheel!!!! – Tiago Guedes Jan 28 '15 at 10:56

1 Answers1

3

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.

FIDDLE

SharmaPattar
  • 2,472
  • 3
  • 21
  • 23