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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:56:04+00:00 2026-06-10T18:56:04+00:00

I have a table: <table> <tr><td>1</td></tr> <tr><td>2</td></tr> <tr><td>3</td></tr> </table> An array that tells where

  • 0

I have a table:

<table>
    <tr><td>1</td></tr>
    <tr><td>2</td></tr>
    <tr><td>3</td></tr>
</table>

An array that tells where every row should come [{index: 2},{index: 1},{index: 0}] (first row is the last from the array, second row is the 1 in array and third row 0 from the array).

  • 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-10T18:56:06+00:00Added an answer on June 10, 2026 at 6:56 pm

    Here is my approach.

    // create a new temporary tbody outside the DOM (similar to jQuery's detach)
    var tbody_tmp   = document.createElement('tbody');
    
    // itterate through the array in the order of a new table
    for(var i = 0, j = data.length; i < j; i++)
    {
        // make a copy of current row (otherwise, append child removes the row from the rows array and messes up the index-finder; there got be a better way for this)
        var row     = rows[data[i].index].cloneNode(true);
    
        tbody_tmp.appendChild(row);
    
        // reset the index to reflect the new table order (optional, outside the sample)
        data[i].index   = i;
    }
    
    // Note that tbody is a jquery object
    tbody.parent()[0].replaceChild(tbody_tmp, tbody[0]);
    

    Though, the cloning approach is slow. With 10,000+ records it takes ~1200ms. Furthermore, a jQuery-less approach is preferable.

    Posting this in case someone else might find it simple enough for their needs (with less than 1,000 rows).


    After hours of restless thinking, I’ve ended up with the following. If this sample isn’t enough, I’ve written a whole blog post explaining the logic behind it, http://anuary.com/57/sorting-large-tables-with-javascript.

    // Will use this to re-attach the tbody object.
    var table       = tbody.parent();
    
    // Detach the tbody to prevent unnecessary overhead related
    // to the browser environment.
    var tbody       = tbody.detach();
    
    // Convert NodeList into an array.
    rows            = Array.prototype.slice.call(rows, 0);
    
    var last_row    = rows[data[data.length-1].index];
    
    // Knowing the last element in the table, move all the elements behind it
    // in the order they appear in the data map
    for(var i = 0, j = data.length-1; i < j; i++)
    {
        tbody[0].insertBefore(rows[data[i].index], last_row);
    
        // Restore the index.
        data[i].index   = i;
    }
    
    // Restore the index.
    data[data.length-1].index   = data.length-1;
    
    table.append(tbody);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a three dimensional bit table array as bit_table[dim1][100][200]; The second and third
Suppose I have an array that mimics a database table. Each array element represents
I want to uapdate a table field with an array.That means i have a
I have a table view that i want to populate with information from xml.
I have fetched the contents of a table in array of object. And the
While working with ActiveRecord I have a table which stores a serialized array of
I've got an array of names: $names = array('ray'=>0,'bob'=>1,'sue'=>2,'jeff'=>3); Then I have a table
Say I have an array of table DDL queries and I want to get
I have written a function to print database table to an array like this
I have a 2 dimensional array forming a table: [color][number][shape ] ------------------------- [black][10 ][square

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.