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

The Archive Base Latest Questions

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

I am trying to sort a table. I’ve seen several jQuery and JavaScript solutions

  • 0

I am trying to sort a table. I’ve seen several jQuery and JavaScript solutions which do this through various means, however, haven’t seen any that use JavaScript’s native sort() method. Maybe I am wrong, but it seems to me that using sort() would be faster.

Below is my attempt, however, I am definitely missing something. Is what I am trying to do feasible, or should I abandon it? Ideally, I would like to stay away from innerHTML and jQuery. Thanks

var index = 0; //Index to sort on.
var a = document.getElementById('myTable').rows;

//sort() doesn't work on collection
var b = [];
for (var i = a.length >>> 0; i--;) {
    b[i] = a[i];
}

var x_td, y_td;

b.sort(function(x, y) {
    //Having to use getElementsByTagName is probably wrong
    x_td = x.getElementsByTagName('td')[index].data;
    y_td = y.getElementsByTagName('td')[index].data;
    return x_td == y_td ? 0 : (x_td < y_td ? -1 : 1);
});
  • 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:09:18+00:00Added an answer on June 14, 2026 at 6:09 am

    A td element doesn’t have a .data property.

    If you wanted the text content of the element, and if there’s only a single text node, then use .firstChild before .data.

    Then when that is done, you need to append the elements to the DOM. Sorting a JavaScript Array of elements doesn’t have any impact on the DOM.

    Also, instead of getElementsByTagName("td"), you can just use .cells.

    b.sort(function(rowx, rowy) {
        x_td = rowx.cells[index].firstChild.data;
        y_td = rowy.cells[index].firstChild.data;
        return x_td == y_td ? 0 : (x_td < y_td ? -1 : 1);
    });
    
    var parent = b[0].parentNode;
    
    b.forEach(function(row) {
        parent.appendChild(row);
    });
    

    If the content that you’re comparing is numeric, you should convert the strings to numbers.

    If they are text strings, then you should use .localeCompare().

    return x_td.localeCompare(y_td);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to sort all of this information into a small table which
I am trying to sort a table with JavaScript. function test() { var table
I am trying to sort a table which has column like 2009-12-17 23:59:59.0 .
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
I am trying to use jQuery's draggable and sortable to sort elements listed inside
I'm trying to follow http://www.foliotek.com/devblog/make-table-rows-sortable-using-jquery-ui-sortable/ And have created a very simple example http://jsfiddle.net/6wbZQ/ It's
Trying to sort this array of objects according to (1) depth and (2) weight,
Been trying to sort this one out for a while. I'd really appreciate any
I am trying to sort an array in PHP by date and time which

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.