Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5848169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:49:16+00:00 2026-05-22T12:49:16+00:00

<html lang=en> <head> <meta http-equiv=Content-Type content=text/html; charset=UTF-8 /> <title>Sort plugin for jQuery</title> </head> <body>

  • 0
<html lang="en"> 
    <head> 
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
            <title>Sort plugin for jQuery</title> 
    </head> 
    <body> 

        <h1>Demo</h1> 

        <p>Click on the headers (fruit/quantity).</p> 

        <table> 
            <thead> 
                <tr> 
                    <th>Fruit</th> 
                    <th>Quantity</th> 
                </tr> 
            </thead> 
            <tbody> 
                <tr> 
                    <td>Grape</td> 
                    <td>15</td> 
                </tr> 
                <tr> 
                    <td>Apple</td> 
                    <td>4</td> 
                </tr> 
              </tbody> 
        </table>       

        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> 
        <script src="jquery.sort.js"></script> 
        <script> 
            var th = jQuery('th'),
                li = jQuery('li'),
                inverse = false;

            th.click(function(){

                var header = $(this),
                    index = header.index();
                header
                    .closest('table')
                    .find('td')
                    .filter(function(){                     
                        return $(this).index() === index;
                    })
                    .sort(function(a, b){

                        a = $(a).text();
                        b = $(b).text();
                         return (
                            isNaN(a) || isNaN(b) ?
                                a > b : +a > +b
                            ) ?
                                inverse ? -1 : 1 :
                                inverse ? 1 : -1;

                    }, function(){
                        return this.parentNode;
                    });

                inverse = !inverse;

            });

        </script> 

    </body> 
</html>

In the above program when I click on the <th> it sorts the rows in that column. It also uses the .sort method from this file.

Can you explain how the above code works, and its inner working? This is the link where I got the above code:

  • http://qd9.co.uk/projects/jQuery-Plugins/sort/demo.html
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T12:49:17+00:00Added an answer on May 22, 2026 at 12:49 pm

    Explanation in comments inline:

    th.click(function(){
    
                var header = $(this), // get the table header as a JQuery object
                    index = header.index(); // get the index - column number - of the header
                header
                    .closest('table') // get the table that contains the header
                    .find('td') // find all the td objects
                    .filter(function(){                     
                        return $(this).index() === index; // but filter that list so that only td's in the relevant column are selected
                    })
                    .sort(function(a, b){ // use the external 'sort' function (JQuery plugin) on our list of table data, and sort using this anonymous function to compare
    
                        a = $(a).text(); // compare the text content of two td elements, using alphabetic or numeric ordering where appropriate
                        b = $(b).text();
                         return (
                            isNaN(a) || isNaN(b) ?
                                a > b : +a > +b
                            ) ?
                                inverse ? -1 : 1 : // and invert the sort order if the 'inverse' flag is true
                                inverse ? 1 : -1;
    
                    }, function(){
                        return this.parentNode; // the sort function returns a list of sorted elements - so returning the td parent returns the row, which means it sorts the table rows
                    });
    
                inverse = !inverse; // toggle the inverse flag so that multiple clicks on the header invert the order
    
            });
    

    Comaprison (sort) Function:

    The comparison function is interesting. To make it more readable consider the following function and example input/output:

    function compare(a, b) {
        return isNaN(a) || isNaN(b) ? a > b : +a > +b;
    }
    
    log("isNaN(\"hi\"):" + isNaN("hi"));
    log("isNaN(8): " + isNaN(8));
    log("isNaN(\"8\"): " + isNaN("8"));
    
    log("compare(\"hi\", \"there\"): " + compare("hi", "there"));
    log("compare(\"there\", \"hi\"): " + compare("there", "hi"));
    
    log("compare(2, 4): " + compare(2, 4));
    log("compare(4, 2): " + compare(4, 2));
    
    log("compare(\"hi\", 2): " + compare("hi", 2));
    

    Output:

    isNaN("hi"):true
    isNaN(8): false
    isNaN("8"): false
    compare("hi", "there"): false
    compare("there", "hi"): true
    compare(2, 4): false
    compare(4, 2): true
    compare("hi", 2): false
    

    Explanation:

    The isNaN function returns true if the input ‘is not a number’ and false otherwise. Conveniently, it considers strings of digits to be numbers. So if we are comparing two numbers (whether strings or not) our comparison function returns

    +a > +b
    

    Appending the + just converts the string to a real javascript numerical object, meaning that the text won’t be alphabetically sorted when the text represents numerical values.

    If either cell’s contents is not a number, then the comparison function returns

    a > b
    

    …which just applies the default > operator for the object. In the case of strings, it will result in sorting the strings alphabetically.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Looking at the example code in the docs http://developer.android.com/reference/android/content/Context.html#getExternalFilesDir%28java.lang.String%29 File path = getExternalFilesDir(Environment.DIRECTORY_PICTURES); It
html Tidy gives this as output for some reason: <?xml version=1.0 encoding=utf-16?> <?xml version=1.0
In HTML in the td of a table you can break text by using
I have this error when I try to include the tag ( http://code.google.com/p/struts2-jquery/wiki/HeadTag )
How can I make the code indent correctly? app/views/layouts/shared.html.haml: = render :partial => shared/head
HTML (or maybe just XHTML?) is relatively strict when it comes to non-standard attributes
Html.TextBox(ParentPassword, , new { @class = required }) what the gosh darned heck is
---HTML <div id=story> <div id=individual> <img src='uploads/1231924837Picture.png'/> <h2>2009-01-14</h2> <h1>Headline</h1> <p>stroy story etc stroy story
Html.Encode seems to simply call HttpUtility.HtmlEncode to replace a few html specific characters with
The HTML standard defines a clear separation of concerns between CSS (presentation) and HTML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.