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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:16:14+00:00 2026-06-01T07:16:14+00:00

I am trying to sort a table with JavaScript. function test() { var table

  • 0

I am trying to sort a table with JavaScript.

function test() {
    var table = document.getElementById("myTable");

    var allRow = table.getElementsByTagName('tr');

    var ar = new Array();
    for(var i = 1; i < allRow.length; i++) {
        ar[i - 1] = allRow[i];
    }

    //sort the data according to number
    ar.sort(function(l, r) {
        var num1 = parseInt(l.childNodes[1].innerHTML);
        var num2 = parseInt(r.childNodes[1].innerHTML);
        return (num1 > num2) ? 1 : ((num1 < num2) ? -1 : 0);
    });
    //assigning the sorted data in the table
    for(var i = 0; i < ar.length; i++) {
        allRow[i + 1].innerHTML = ar[i].innerHTML;
    }
}

But when I am trying to assigning the sorted data in the table somehow it does not. But If I keep the ar data in a new array and then assign that in allRow it does. I am totally newbie in this. So can anyone please tell me why is this happening?

var tmp = new Array();
for(var i = 0 ; i<ar.length ; i++){
    tmp[i] = ar[i].innerHTML;
}

for(var i = 0; i < ar.length; i++) {
    allRow[i + 1].innerHTML = tmp[i];
}
  • 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-01T07:16:16+00:00Added an answer on June 1, 2026 at 7:16 am

    When you copy the elements to the ar array you are not making copies of the elements, you are only copying the references to the elements. The ar array doesn’t contain elements with the same content as the allRow array, it contains the same elements.

    When you change the content of an element in the allRow array, it will change in the ar array also, as it’s the same element.

    When you have sorted the array, read the innerHTML from all rows, then you can write them back:

    for(var i = 0; i < ar.length; i++) {
      ar[i] = ar[i].innerHTML;
    }
    for(var i = 0; i < ar.length; i++) {
      allRow[i + 1].innerHTML = ar[i];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to reference a JavaScript file, containing a table sort function, in
I'm trying to sort a table with javascript Date objects in one of the
Trying to sort this array of objects according to (1) depth and (2) weight,
I'm trying to sort a multidimensional array in objective-c i know that i can
i'm trying to pass a javascript array to a php controller ( i'm using
I am new to programming especially jQuery. I am trying to sort (order) images
I am trying to sort the table column values by clicking on the header
I've been trying to sort a table using ajax for sometime now but the
I am trying to write some front end Javascript tests that can test drag
<script type=text/javascript> $(function() { $( #sortable ).sortable({ update: function(event, ui) { var Order =

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.