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

  • SEARCH
  • Home
  • 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 5934311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:05:47+00:00 2026-05-22T15:05:47+00:00

I am using James Padolsey’s jQuery sorting script for a HTML table in an

  • 0

I am using James Padolsey’s jQuery sorting script for a HTML table in an ASP.NET page. I can’t use other table sorting scripts due to some template restrictions.

http://james.padolsey.com/javascript/sorting-elements-with-jquery/

Here is the script that I use:

(function($){  
    $.fn.sortElements = (function(){

        var sort = [].sort;

        return function(comparator, getSortable) {

            getSortable = getSortable || function(){return this;};

            var placements = this.map(function(){

                var sortElement = getSortable.call(this),
                    parentNode = sortElement.parentNode,

                // Since the element itself will change position, we have
                // to have some way of storing its original position in
                // the DOM. The easiest way is to have a 'flag' node:
                nextSibling = parentNode.insertBefore(
                    document.createTextNode(''),
                    sortElement.nextSibling
                );

                return function() {

                    if (parentNode === this) {
                        throw new Error(
                            "You can't sort elements if any one is a descendant of another."
                        );
                    }

                    // Insert before flag:
                    parentNode.insertBefore(this, nextSibling);
                    // Remove flag:
                    parentNode.removeChild(nextSibling);

                };

            });

            return sort.call(this, comparator).each(function(i){
                placements[i].call(getSortable.call(this));
            });

        };

    })();

    $.fn.tablesort = (function(options) {
        return this.each(function() {  
            var table = $(this);
            $(this).find('thead th').wrapInner('<a href="#"/>').find('a').click(function(){
              var sort = $(this).data('sort');
              $(this).parents('thead').find('a').removeClass('sort-asc sort-desc');
              sort = (sort=='asc'? 'desc' : (sort=='desc'? 'asc' : 'asc'));
              $(this).data('sort', sort).addClass('sort-'+sort);
              table.find('tbody tr td').removeClass('column-selected');
              table.find('tbody tr td:nth-child('+($(this).parent().index()+1)+')').sortElements(
                function(a, b){
                    return sort=='desc'? ($(a).text() < $(b).text()) - ($(a).text() > $(b).text()) : ($(a).text() > $(b).text()) - ($(a).text() < $(b).text());
                },
                function(){
                    return this.parentNode; 
                }
              ).addClass('column-selected');
              return false;
            });
            return $(this);
        });
    });

})(jQuery);

And, here is the simplified table code:

<table class="datatable paginate sortable full">
<thead>
<tr>
<th>Product Name</th>
<th>Product Type</th>
<th>Assembled On</th>
</tr>
</thead>
<tbody>
<tr>
<td>
...
</td>
</tr>
</table>

The first two columns are being sorted correctly, but the 3rd column isn’t sorting properly.

3rd column is a date field and the format is MM/DD/YYYY (like 3/12/2009 and 3/9/2009).

In the table, proper sorting should be like 2/21/2009, 3/9/2009 and 3/12/2009; but the current script makes it: 2/21/2009, 3/12/2009 and 3/9/2009.

I tried adding some parsers without any success. How can I fix this issue?

PS: I am not a jQuery expert.

Thank you for all of the help.

  • 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-22T15:05:48+00:00Added an answer on May 22, 2026 at 3:05 pm

    Add a class to designate a column as a date …

    <table class="datatable paginate sortable full">
    <thead>
    <tr>
    <th>Product Name</th>
    <th>Product Type</th>
    <th class="date">Assembled On</th> <!-- added a class="date" to mark this col as date -->
    </tr>
    </thead>
    

    then alter the function a little bit to

    $.fn.tablesort = (function(options) {
        return this.each(function() {  
            var table = $(this);
            $(this).find('thead th').wrapInner('<a href="#"/>').find('a').click(function(){
              var sort = $(this).data('sort');
              var isColDate = $(this).parent().hasClass("date");
    
              $(this).parents('thead').find('a').removeClass('sort-asc sort-desc');
              sort = (sort=='asc'? 'desc' : (sort=='desc'? 'asc' : 'asc'));
              $(this).data('sort', sort).addClass('sort-'+sort);
              table.find('tbody tr td').removeClass('column-selected');
              table.find('tbody tr td:nth-child('+($(this).parent().index()+1)+')').sort(
                function(a, b){
    
                    if (isColDate) {
                        a = new Date($(a).text());
                        b = new Date($(b).text());
                    }
                    else {
                        a = $(a).text();
                        b = $(b).text();
                    }
    
                    return sort=='desc'? (a < b) : (a > b);
                },
                function(){
                    return this.parentNode; 
                }
              ).addClass('column-selected');
              return false;
            });
            return $(this);
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using preview 4 of ASP.NET MVC Code like: <%= Html.CheckBox( myCheckBox, Click Here, True,
Ok, here's my problem. Im using this plugin (http://james.padolsey.com/javascript/jquery-plugin-autoresize/) to autoresize my textarea when
I'm using jquery 1.5.1, James Padolsey Cross Domain Ajax 0.11 which uses YQL to
Using PyObjC , you can use Python to write Cocoa applications for OS X.
Using ASP.NET MVC there are situations (such as form submission) that may require a
Using VS2008, C#, .Net 2 and Winforms how can I make a regular Button
Bill James wrote : I was able to render an HTML page with the
I am using the mimic igoogle tutorial with cookies by James Padolsey http://james.padolsey.com/tag/cookies/ I
I'm trying to perform a simple html request using jquery with the code below.
I am using the JoeBlogs .Net wordpress wrapper by Alex James Brown. It just

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.