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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:01:39+00:00 2026-06-13T13:01:39+00:00

I need to sort the rows in a table using the number in an

  • 0

I need to sort the rows in a table using the number in an anchor (anchor that not exist in some rows). Table example:

<table id="sortedtable" class="full">
    <tbody>
        <tr>
            <th>Main tr</th>
        </tr>
        <tr>
            <td>
                <div style="float: left;">Title</div>
                <a class="unreadcount" href="#">7</a>
            </td>
        </tr>
        <tr>
            <td>
                <div style="float: left;">No anchor here :(</div>
            </td>
        </tr>
        <tr>
            <td>
                <div style="float: left;">Javascripthatesme</div>
                <a class="unreadcount" href="#">15</a>
            </td>
        </tr>
        <tr>
            <td>
                <div style="float: left;">Yourox</div>
                <a class="unreadcount" href="#">2</a>
            </td>
        </tr>
        <tr>
            <td>
                <div style="float: left;">This has more than one word and no anchor</div>
            </td>
        </tr>
        <tr>
            <td>
                <div style="float: left;">Title</div>
                <a class="unreadcount" href="#">11</a>
            </td>
        </tr>
        <tr>
            <td>
                <div style="float: left;">Big Bang Theory is not funny</div>
                <a class="unreadcount" href="#">4</a>
            </td>
        </tr>   
    </tbody>
</table>

Using the solution in this question I was able to make a semi-working code:

var $table = $('table');

var rows = $table.find('tr').get();
rows.sort(function(a, b) {
    var keyA = $(a).find('a.unreadcount').text();
    var keyB = $(b).find('a.unreadcount').text();
    if (keyA < keyB) return 1;
    if (keyA > keyB) return -1;
    return 0;
});
$.each(rows, function(index, row) {
    $table.children('tbody').append(row);
});

You can see it working in this jsFiddle. The problem and where I need help is the way numbers are sorted (8 come first than 32 or 12, for example) and that I need to ignore the first row (where “Main tr” is) and leave it always at the top.

And as an extra, if you could give me the direction to make a button that put the table in the original order again would be awesome.

Thanks.

  • 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-13T13:01:40+00:00Added an answer on June 13, 2026 at 1:01 pm

    You need to convert the text values to numbers for a proper numeric sort. The easies way to convert is with the unary plus operator.

    To leave the heading row in place just don’t include that in the array you sort, which is easy enough using jQuery’s .slice() method.

    To add a button that restores the table to its original order just keep a copy of the array before you sort it, which I’ve done with the standard Array .slice() method.

    So:

    var $table = $('table');
    
    var originalRows = $table.find('tr').slice(1).get(),
        rows = originalRows.slice(0);
    rows.sort(function(a, b) {
        var keyA = +$(a).find('a.unreadcount').text();
        var keyB = +$(b).find('a.unreadcount').text();
        if (keyA < keyB) return 1;
        if (keyA > keyB) return -1;
        return 0;
    });
    $.each(rows, function(index, row) {
        $table.children('tbody').append(row);
    });
    
    // button that changes it back:
    $("input").click(function(){
        $.each(originalRows, function(index, row) {
           $table.children('tbody').append(row);
        });
    });
    ​
    

    Demo: http://jsfiddle.net/hLvwz/5/

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

Sidebar

Related Questions

I have a temp table variable that I need to update with something sort
I need to sort an array that can look like this: $array[4][0] = array('id'
I need to sort then print the result increasing and decreasing. I have some
The Problem I have a table that's about 2 million rows (at 115 MB)
When I try to sort rows on below table after clicking the append button
I have an HTML table with less than 50 rows. I am using Sorttable
I'm using MySQL. I have a table where I need to be able to
I have a table of data that contains about 260 rows. Each of these
I need to sort a flat file by third column leaving first column intact
I need to sort list of strings in the alphabetical order: List<String> list =

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.