Search code examples
jqueryhtmltablesorter

How to sort table in ascending order first on change of sorting column using tablesorter in jquery?


How to sort table in ascending order first on change of sorting column using tablesorter in jquery?

If I click the first column once it will sort in ascending order, then If I go to the second column and click it, it should sort in ascending order. If I click the column twice then only it should sort in descending order.

How can I do that?


Solution

  • In the tablesorter code, each column has it's sort order stored in the header element. By default, the sort is set to ascending on first click, then descending on second.

    I don't know which version of tablesorter you are using, but if you are using the original from tablesorter.com (v2.0.5), then there is a global option that isn't listed in the documentation (see this post on missing docs) named sortInitialOrder which can be set to either "asc" or "desc" to change the sort direction on first click.

    If you are using my fork of tablesorter, you can still use the global sortInitialOrder option or apply it to specific columns by using the headers option. Additionally, there is a sortRestart option which forces the first click on any column to always use the sortInitialOrder first.

    Another option that might interest you is the sortReset option which makes the third click on a header restore the table to its initial unsorted state.