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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:46:05+00:00 2026-06-18T12:46:05+00:00

Assuming I have a collection with a bunch of well ordered elements, what’s the

  • 0

Assuming I have a collection with a bunch of well ordered elements, what’s the common method to insert another new child at it’s abstract order-position?

Using a dom library $(new).eq($(new).data('orderPosition')); doesn’t work, because it’s not a valid index.

// Add this element to it's logic position in the collection:
<div data-order-position="10"></div>

// The collection
<div id="myCollection">
   <div data-order-position="4"></div>
   <div data-order-position="67"></div>
   <div data-order-position="81"></div>
   <div data-order-position="82"></div>
   <div data-order-position="761"></div>
</div>

My real collection contains about ~400 elements.

  • 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-18T12:46:06+00:00Added an answer on June 18, 2026 at 12:46 pm

    I think that working with an array of integers is probably the most efficient method. You can maintain a constant list of the sorted elements in an array somewhere (and even continue to sort as needed):

    //Array of positions
    var positions = [];
    
    //Initially set up the array in question
    //divs are already sorted, as we know
    $("#myCollection div").each(function () {
       positions.push(parseInt(this.dataset.orderPosition)); 
    });
    
    //Create the new node we want to insert (in your case it may already exist)
    var $new = $("<div>").data('order-position', 10).text(10);
    
    //Append the new node index (if node already exists, just use `.data` as above)
    positions.push(10);
    
    //Yes, for whatever reason JS sorts by string and not number by default.
    //There may also be a more efficient way to add the integer in the correct spot
    //without having to sort all over again, but this is fast enough
    positions.sort(function (a, b) {
        return a - b;
    });
    
    //Insert the new node!
    $("#myCollection div").eq(positions.indexOf(10) - 1).after($new);
    

    http://jsfiddle.net/ExplosionPIlls/ULEFX/

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

Sidebar

Related Questions

I have a collection I'm unable to drop, I'm assuming that the - in
I have a bunch of collection-like COM interfaces that I'm trying to write an
I have a collection of <li> elements which are aligned so that they form
I have web page that has a collection of tiled elements as a background.
Using Java, assuming v1.6. I have a collection where the unique index is a
Let's say I have a collection of objects. I have another collection of likes,
I need to implement a prioritised collection. Assuming we have the following three values
I have a bunch of javascript functions, that trigger upon button clicks. So assuming
Assuming I have a collection called posts (in reality it is a more complex
Assuming I have a collection in MongoDB with 5000 records, each containing something similar

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.