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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:41:28+00:00 2026-06-14T06:41:28+00:00

Here is my first display of the page(actually the data in rows and columns

  • 0

Here is my first display of the page(actually the data in rows and columns are generated from server code and database). Please also pay attention to the attributes of each data

  <table>
        <tr>
            <td id="1" class="decorate">Item3</td>
            <td class="decorate"><p>Description<p></td>
        </tr>
        <tr>
            <td class="decorate">Item1</td>
            <td class="decorate"><p>Description1</p><p>Description2</p></td>
        </tr>
        <tr>
            <td id="2" class="decorate">Item2</td>
            <td class="decorate"><p>Description3<p></td>
        </tr>
    </table>
                <div id="tablediv"></div>

And here is the js code to read that data on OnLoad event and append the data into “tablediv” after they are sorted.

$(document).ready(function() {
    var arr = new Array();
    $('table tr').each(function() {
        var $row = $(this);
        var key = $row.find('td:first').html();
        var value = $row.find('td:last').html();
        console.log(value);
        arr.push([key, value]);
    });

    arr.sort(function(a, b) {
        var valueA, valueB;
        valueA = a[0]; // sort by the first value of array
        valueB = b[0];
        if (valueA < valueB) {
            return -1;
        }
        else if (valueA > valueB) {
            return 1;
        }
        return 0;
    });

    var root=document.getElementById('tablediv');
    var table=document.createElement('table');
    var tbo=document.createElement('tbody');
    var row, cell;
    for(var i=0;i<arr.length;i++)
    {
        row=document.createElement('tr');
        for(var j=0;j<2;j++)
        {
            cell=document.createElement('td');
            cell.appendChild(document.createTextNode(arr[i][j]));
            row.appendChild(cell);
        }
        tbo.appendChild(row);
    }
    table.appendChild(tbo);
    root.appendChild(table);
});

However, I don’t like to put it in the tablediv but to completely replace the previous table (the previous one has no id or class for me to tell where it is in jquery code).

How can I do this ? And that I also would want to retain attributes inside previous table’s td. My array use here need more implementation…:-(

  • 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-14T06:41:30+00:00Added an answer on June 14, 2026 at 6:41 am

    Just sort in place: http://jsfiddle.net/CrossEye/T44Nn/

    var $table = $("table")
    var rows = $table.find("tr").toArray();
    rows.sort(function(a, b) {
        var left = $(a).find("td:first").html(), // .text() ??
            right = $(b).find("td:first").html();
        return left < right ? -1 : left > right ? +1 : 0;
    });
    $(rows).each(function() {
        $(this).appendTo($table);
    });​
    

    This should mean that you don’t have to copy anything from table to table, just reuse the rows intact.

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

Sidebar

Related Questions

my first question here! I have a problem with a piece of code that
Here is the first part of my controller code: public class ControlMController : Controller
I am trying to display content of two sequences on web page. First one
Here's what I've made so far: // somewhere in the page code... <img alt=
Please someone Fix this Javascript Code. This script actually reads the URL Parameter and
Having a bit of CSS problem here. First off, my HTML code: <p> <span
First here's what I'm using and trying to do: the minimal setup for this
First Question in here :) (AS3) First off, how do I access a variable
This is my first here in Stackoverflow. So I just want to ask question
My first post here :) I have a summer job doing a bit of

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.