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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:44:37+00:00 2026-06-13T13:44:37+00:00

A moment ago I asked this question: javascript sort array to align with order

  • 0

A moment ago I asked this question:
javascript sort array to align with order array based on what turned out to be a false premise. (I’ve been up for too long, give me a break please).

What I really need to do is sort DOM elements. Performance is key in my application as it will be running in very CPU-poor situations. So what I have is something like this:

<div id="moduleWrap">
    <div class="module">Jack</div>    <!-- 0 -->
    <div class="module">Jill</div>    <!-- 1 -->
    <div class="module">Nancy</div>   <!-- 2 -->
    <div class="module">Tom</div>     <!-- 3 -->
    <div class="module">Cartman</div> <!-- 4 -->
</div>

And in JavaScript:

$modules = $('#moduleWrap .module');
order    = [3,1,4,0,2];

What I need is this:

<div id="moduleWrap">
    <div class="module">Tom</div>     <!-- 3 -->
    <div class="module">Jill</div>    <!-- 1 -->
    <div class="module">Cartman</div> <!-- 4 -->
    <div class="module">Jack</div>    <!-- 0 -->
    <div class="module">Nancy</div>   <!-- 2 -->
</div>

I thought (wrongly) that I could sort the jQuery object and simply append the result. This is not so.

  • 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-13T13:44:38+00:00Added an answer on June 13, 2026 at 1:44 pm

    I would just iterate over the order array and rearrange the DOM elements accordingly:

    var elements = [];
    $.each(order, function(i, position) {
        elements.push($modules.get(position));
    });
    
    $('#moduleWrap').append(elements);
    

    DEMO

    To increase performance, you can take away more and more jQuery. For example:

    var wrapper = document.getElementById('moduleWrap');
    for(var i = 0, l = order.length; i < l; i++) {
        wrapper.appendChild($modules.get(order[i]));
    }
    

    DEMO

    jQuery is great because it makes things easy, but if you need high performance, you should rather go without it.

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

Sidebar

Related Questions

I just asked this question a moment ago but I asked incorrectly so I
a while ago i asked this question Jquery tabs keep tab open that is
Some time ago I asked a question about nested loops on SO and as
(note this is similar to but not the same as the question I asked
Someone asked a question a few moments ago that got recalled because it was
So I saw this question a few moments ago on SO and it got
A few weeks ago I asked exactly the same question here . At first,
I asked a question a relating to Egit a few weeks ago & it
I noticed a moment ago that my .gitconfig -file was public at my repo.
I just a moment ago saw a request for finding the MAC adress 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.