0

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

2
  • 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. Commented Jan 28, 2015 at 10:47
  • First rule of coding: Don't Reinvent The Wheel!!!! Commented Jan 28, 2015 at 10:56

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.