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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:54:36+00:00 2026-06-13T10:54:36+00:00

Suppose I have a table with fields Rank , Id and Name On clicking

  • 0

Suppose I have a table with fields

Rank, Id and Name

On clicking the Rank , the table gets sorted by rank in ascending order using this code

$(function() {
    $("#rank").click(function() {
        var rows = $("#rank_table tbody tr").get();
        rows.sort(sortTable);
        $.each(rows, function(index, row) {
            $("#rank_table").children("tbody").append(row);
        });
    });
});

function sortTable(a, b) {
    var A = parseInt($(a).children('td').eq(0).text());
    var B = parseInt($(b).children('td').eq(0).text());
    if (A < B) return -1;
    if (A > B) return 1;
    return 0;
}

Rank and Id are integers with id rank and st_id respectively.
So, What I want to achieve is that when I click on Rank field once, table gets sorted in ascending order and again clicking it sorts the table in descending order.

I want to do this for both the fields- rank and Id. For descending, do I need to use a different function other than ascending this ascending function.

How can I achieve this using jQuery and this sort() function(not plugins)?

Here is the html

<!Doctype html>
<html>
<head>
    <style>
        #thead {
            cursor: pointer;
            text-decoration: underline;
            text-align: center;
        }

        #tbody {
            text-align: center;
        }
    </style>
    <script src="libs/jquery-1.5.1.js" type="text/javascript"></script>
    <script src="table_sort.js" type="text/javascript"></script>
</head>

<body>
    <table id="rank_table">
        <thead id="thead">
            <tr>
                <th id="rank">Rank</th>
                <th id="st_id">Student_id</th>
                <th id="st_name">Name</th>
            </tr>
        </thead>
        <tbody id="tbody">
            <tr>
                <td>3</td>
                <td>2</td>
                <td>Ted</td>
            </tr>
            <tr>
                <td>2</td>
                <td>1</td>
                <td>John</td>
            </tr>
            <tr>
                <td>5</td>
                <td>4</td>
                <td>Neil</td>
            </tr>
            <tr>
                <td>1</td>
                <td>5</td>
                <td>Alex</td>
            </tr>
            <tr>
                <td>4</td>
                <td>3</td>
                <td>Nave</td>
            </tr>
        </tbody>
    </table>

</body>

</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-06-13T10:54:37+00:00Added an answer on June 13, 2026 at 10:54 am

    If your current function sorts it ascending, you should only have to change your if statements to get it descending?

    if (A > B) return -1;
    if (A < B) return 1;
    

    This works the way you want:

    $(function() {
        $("#rank").on('click', function() {
            var rows = $("#rank_table tbody tr").get();
            rows.sort(sortTable);
            $.each(rows, function(index, row) {
                $("#rank_table").children("tbody").append(row);
            });
            if (ascending) {
                ascending = false;
            } else {
                ascending = true;
            }
        });
    });
    
    var ascending = false;
    
    function sortTable(a, b) {
        var A = parseInt($(a).children('td').eq(0).text(), 10);
        var B = parseInt($(b).children('td').eq(0).text(), 10);
    
    
        if (ascending) {
            if (A > B) return 1;
            if (A < B) return -1;
        } else {
            if (A > B) return -1;
            if (A < B) return 1;
        }
        return 0;
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose i have a mysql table name table with fields rank date id The
Suppose you have these tables: Table Name: Salesman Fields: S_ID(Primary Key), Name Table Name:
Suppose I have this table: id | name | city ------------------ 1 | n1
I have a table name wishlist . In this I have two fields user_id
Suppose i have a table with 3 fields Person_id, Name and address. Now the
Suppose that i have a table 't' with fields 'id' int 11, 'name' varchar
Suppose I have table in my DB schema called TEST with fields (id, name,
Suppose you have a table with 2 fields, like first name and last name.
Suppose I have this table parent | child 1 2 1 3 2 4
Suppose I have a table that has three integer fields and one string field

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.