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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:02:08+00:00 2026-06-11T10:02:08+00:00

I am trying to sort a dynamically constructed table on the client side. So

  • 0

I am trying to sort a dynamically constructed table on the client side. So far I have done my research to discover JavaScript’s sort() method will take a callback. Here is what I have so far:

function retrvCatalog(e){
var merch = document.getElementById('merch');
var tRows = merch.rows;
var tBody = merch.tBodies;
var rowArr = [];
for (x in tRows){
    rowArr[x] = tRows[x];
}
rowArr.sort(function(a, b){
    if (a.cells.textContent < b.cells.textContent){
        return -1;
    }
    if(a.cells.textContent > b.cells.textContent){
        return 1;
    }
    return 0;
});
}

Stepping through it in Firebug, it appears to not change the order of the rows. Can someone please help me figure out what I am missing?

FINAL ALGORITHM

function retrvCatalog(e){
var fltr = e.id;
var merch = document.getElementById('merch');
var tblHead = merch.tHead;
merch.deleteTHead();
var tRows = merch.rows;
var rowArr = [];
for (var i=0; i<tRows.length; i++){
    rowArr[i] = tRows[i];
}
rowArr = rowArr.sort(function(a, b){
    if (fltr > 3){
        a = parseFloat(a.cells[fltr].innerHTML);
        b = parseFloat(b.cells[fltr].innerHTML);
    }
    else{
        a = a.cells[fltr].innerHTML;
        b = b.cells[fltr].innerHTML;
    }
    if (a>b){
        return 1;
    }
    if(a<b){
        return -1;
    }
    return 0;
});
while(merch.hasChildNodes()) {
    merch.removeChild(merch.firstChild);
}
merch.appendChild(tblHead);
for (i=0;i<rowArr.length;i++){
    merch.appendChild(rowArr[i]);
}
}

The final two columns in the row are numbers, so that is why the method to sort is slightly variable.

  • 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-11T10:02:09+00:00Added an answer on June 11, 2026 at 10:02 am

    I’m not sure if I’m missing something, but I’m pretty sure you can’t use textContent on the cells array. You need to index cells so you know which column to actually sort on. If your rows have 4 columns each (or even if there’s only 1), you still need to tell the sort function which column to sort on.

    So in your sort function, if you wanted to sort by the second column, you’d want something like:

    rowArr.sort(function (a, b) {
        if (a.cells[1].textContent < b.cells[1].textContent) {
            return -1;
        } else if (a.cells[1].textContent > b.cells[1].textContent) {
            return 1;
        }
        return 0;
    });
    

    And I’m not sure what’s in your cells, but you may want to use .innerHTML, not .textContent.

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

Sidebar

Related Questions

So I have a std::vector<std::unique_ptr<Base>> vec and I'm trying to sort it dynamically, given
I have a MySQL query I'm trying to sort dynamically when a link is
I'm trying to sort by the state of an Organization's main address. I have
I'm trying to sort data by different fields ascending and descending. But I have
I have a WPF application that I'm trying to dynamically add items to a
I'm trying to figure out how to pass 2D array, which is constructed dynamically
I'm trying to transform one method call into another dynamically (at runtime). For instance,
I'm trying to dynamically change (if it got clicked) a normal table header (which
I'm trying to bake out a sort of single table inheritence a.k.a. table per
I am trying to get a UIWebView to update dynamically, sort of like a

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.