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 8459423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:19:35+00:00 2026-06-10T13:19:35+00:00

I have a task of sorting the table row in a table. The data

  • 0

I have a task of sorting the table row in a table. The data in the table is a mixture of everything like date, number, string etc etc.

I have gone through many links where I found some are directed to the ready library. Which is of no use to me. Finally going through lot of things I have made my own using all tits and bits. which is working only for number

This is the script:

  $(document).ready(function () {

        //grab all header rows
        $('th').each(function (column) {
            $(this).addClass('sortable').click(function () {
                    var findSortKey = function ($cell) {
                        return $cell.find('.sort-key').text().toUpperCase()+ ' ' + $cell.text().toUpperCase();

                    };
                    var sortDirection = $(this).is('.sorted-asc') ? -1 : 1;
                    var $rows = $(this).parent().parent().parent().find('tbody tr').get();

                    //loop through all the rows and find
                    $.each($rows, function (index, row) {
                        row.sortKey = findSortKey($(row).children('td').eq(column));
                    });

                    //compare and sort the rows alphabetically or numerically
                    $rows.sort(function (a, b) {
                        if (a.sortKey.indexOf('-') == -1) {
                            if (parseInt(a.sortKey) < parseInt(b.sortKey)) {
                                return -sortDirection;
                            }
                            if (parseInt(a.sortKey) > parseInt(b.sortKey)) {                                
                                return sortDirection;
                            }
                        } else {
                            if (a.sortKey < b.sortKey) {
                                return -sortDirection;
                            }
                            if (a.sortKey > b.sortKey) {
                                return sortDirection;
                            }
                        }
                        return 0;
                    });

                    //add the rows in the correct order to the bottom of the table
                    $.each($rows, function (index, row) {
                        $('tbody').append(row);
                        row.sortKey = null;
                    });

                    //identify the column sort order
                    $('th').removeClass('sorted-asc sorted-desc');
                    var $sortHead = $('th').filter(':nth-child(' + (column + 1) + ')');
                    sortDirection == 1 ? $sortHead.addClass('sorted-asc') : $sortHead.addClass('sorted-desc');

                    //identify the column to be sorted by
                    $('td').removeClass('sorted').filter(':nth-child(' + (column + 1) + ')').addClass('sorted');
                });
            });
        });

This is the style for the document:

    <style>
    root
    {
        display: block;
    }

    th.sortable
    {
        color: #666;
        cursor: pointer;
        text-decoration: underline;
    }

        th.sortable:hover
        {
            color: black;
        }

    th.sorted-asc, th.sorted-desc
    {
        color: black;
        background-color: cadetblue;
    }
</style>

So below is the HTML part.

<table>
    <tbody>
        <tr>
            <th class="sortable">Name</th>
            <th class="sortable">Salary</th>
            <th>Extension</th>
            <th>Start date</th>
            <th>Start date (American)</th>
        </tr>
        <tr>
            <td>Bloggs, Fred</td>
            <td>$12000.00</td>
            <td>1353</td>
            <td>18/08/2003</td>
            <td>08/18/2003</td>
        </tr>
        <tr>
            <td>Turvey, Kevin</td>
            <td>$191200.00</td>
            <td>2342</td>
            <td>02/05/1979</td>
            <td>05/02/1979</td>
        </tr>
        <tr>
            <td>Mbogo, Arnold</td>
            <td>$32010.12</td>
            <td>2755</td>
            <td>09/08/1998</td>
            <td>08/09/1998</td>
        </tr>
        <tr>
            <td>Shakespeare, Bill</td>
            <td>$122000.00</td>
            <td>3211</td>
            <td>12/11/1961</td>
            <td>11/12/1961</td>
        </tr>
        <tr>
            <td>Shakespeare, Hamnet</td>
            <td>$9000</td>
            <td>9005</td>
            <td>01/01/2002</td>
            <td>01/01/2002</td>
        </tr>
        <tr>
            <td>Fitz, Marvin</td>
            <td>$3300</td>
            <td>5554</td>
            <td>22/05/1995</td>
            <td>05/22/1995</td>
        </tr>
    </tbody>
</table>
  • 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-06-10T13:19:37+00:00Added an answer on June 10, 2026 at 1:19 pm

    Sounds like homework, which is fine, as you have shown some effort yourself. If you are not allowed to tablesorter , download it, I think there is a debug version, and have a look at the code. That should help you in your quest.

    If this is homework, remember out in the real world that a golden rule of programming is don’t re-invent the wheel. If there is a plug in that meets your needs, use it.

    Fixing What you have

    The problem with your code as it stands is:

    if (a.sortKey.indexOf('-') == -1) {
        if (parseInt(a.sortKey) < parseInt(b.sortKey)) {
            return -sortDirection;
         }
    
         if (parseInt(a.sortKey) > parseInt(b.sortKey)) {                                
              return sortDirection;
         }
    } else {
        //Non numeric sort
    }
    

    Here you are always trying to sort as an integer if a does not contain -. A very rough fix is:

    if (a.sortKey.indexOf('-') == -1 && (!isNaN(a.sortKey) && !isNaN(a.sortKey))) {
    //Rough Numeracy check                          
    
        if (parseInt(a.sortKey) < parseInt(b.sortKey)) {
            return -sortDirection;
        }
    
        if (parseInt(a.sortKey) > parseInt(b.sortKey)) {                                
            return sortDirection;
        }
    
     } else {
        //Non numeric sort
     }
    

    Here is a working fidle.

    Keep in mind this is a very rough numeracy check you will probably need other checks and balances for other data types.

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

Sidebar

Related Questions

I have the task of creating implementations for a large number of metric data
G'day there! I have task like show how many time was gone from message
I have a task to do, I need to take data that has been
I have a task like this... def self.perform(parsed_json) do_hard_work use_faye_to_push_status rescue => exception use_faye_to_push_error
I have a 2D string array consisting of values like as { Home,0.1256784 Contact,-0.56789
I have 7 tables. Each table represents transmission type where each row in table
I have task that involves creating the right design for the JSON string before
i have task which takes a parameter and has three modes of results Example
I have task involving reading SAS .xpt files using .NET. For that I'm using
I'm using dhtmlx Gantt Chart UI component which have task list and graphical chart.

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.