Client Side Sorting With JavaScript

Discussion in 'JavaScript and AJAX' started by pradeep, Aug 8, 2012.

  1. pradeep

    pradeep Team Leader

    Apr 4, 2005
    Likes Received:
    Trophy Points:
    Kolkata, India
    Home Page:
    JavaScript, the most popular & most used client-side scripting language on the internet is really powerful in creating rich user experience by itself. One of the best example of JavaScript excellence is the jQuery library which has helped developers all over the world to build the business logic instead of looking into the nuances of event handlers, browser compatibility etc. Tablesorter is a jQuery based plugin which allows developers to enabled users sort data in tables by columns, by data type and it even allows you to define your own type.

    In this article we'll look into the Tablesorter plugin so you might use it in your application.

    The Basics

    The basic requirement of Tablesorter plugin is that you have jQuery (1.2.1+), who doesn't these days, and in case you don't already use jQuery, try it, it's worth it. Download tablesorter from

    To your page HTML add the following lines of code in the HEAD section.

    <script type="text/javascript" src="/your-path-to/js/jquery-latest-min.js"></script> 
    <script type="text/javascript" src="/your-path-to/js/jquery.tablesorter.js"></script> 
    Add your HTML table, and call the tablesorter function like this.

      <title>Programming Languages</title>
      <script type="text/javascript" src="/js/jquery.js"></script>
      <script type="text/javascript" src="/js/jquery.tablesorter.js"></script>
      <script language="text/javascript">
      $(document).ready(function() {
      <table id="ProgLangs">
                <th>Sl. No.</th>
    That's it, your table will be ready for client-side sorting.


    You may change default behaviour of the tablesorter, disallow certain columns from being sorted on, etc. Please follow the code examples below:

    // Restrict 1st column from being sorted on
    $('#ProgLangs').tablesorter( { headers: { 0: { sorter: false} } } );
    // Setting an initial sorting order, say sort on the 3rd column in ascending order
    $('#ProgLangs').tablesorter( { headers: { 0: { sorter: false} }, sortList: [ [2,0] ]  } );
    // Add a pager to show pagination, you will need extra HTML for the pager
    $('#ProgLangs').tablesorter( { headers: { 0: { sorter: false} },  sortList: [ [2,0] ]  } ).tablesorterPager({container: $("#pager")}); 
    // Add your own sorter, say we would like to sort language name on string length
        // set an unique id 
        id: 'sortByLen', 
        is: function(s) { 
            return false; 
        format: function(s) { 
            // format your data for sorting
            return s.length(); 
        // set type, numeric or text 
        type: 'numeric'
    $("#ProgLangs").tablesorter({ headers: { 1: { sorter:'sortByLen' } } }); 
    ManzZup likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice