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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:17:22+00:00 2026-06-08T17:17:22+00:00

I have a sorted (Mongodb) collection of objects: {_id: ObjectId(…), person: Homer, order: 1},

  • 0

I have a sorted (Mongodb) collection of objects:

{"_id": ObjectId(...), "person": "Homer", "order": 1},
{"_id": ObjectId(...), "person": "Marge", "order": 2}

I would like to prepend, append or insert between two objects another object. I’ve tried this using float numbers like this:

{"_id": ObjectId(...), "person": "Homer", "order": 1},
{"_id": ObjectId(...), "person": "Bart", "order": 1.5},
{"_id": ObjectId(...), "person": "Marge", "order": 2}

Order value is the arithmetic mean of fields order in the previous and the next object. It works works as long as numbers don’t look like 1.9999999. Then it is rounded to 2 and the collection isn’t sorted.
I could use integers in this way:

{"_id": ObjectId(...), "person": "Homer", "order": 1},
{"_id": ObjectId(...), "person": "Bart", "order": 2},
{"_id": ObjectId(...), "person": "Marge", "order": 3}

but it means the need to modify the other objects and I would rather avoid that.

I thought about using Strings and lexical sorting to prevent order:

{"_id": ObjectId(...), "person": "Homer", "order": aa},
{"_id": ObjectId(...), "person": "Bart", "order": ab},
{"_id": ObjectId(...), "person": "Marge", "order": bb}

but it seems difficult to implement. Is there any easier way to do it?

  • 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-08T17:17:24+00:00Added an answer on June 8, 2026 at 5:17 pm

    I think that the floats approach makes the most sense.
    Assuming that you start off with all locations as integers, it would take a lot of swaps until you run into precision problems.

    I suggest that whenever you change the “order” of an item, see if it’s too close to one of its neighbors. If it is, reassign the values of all of the objects (so they are all integers again).

    If you set a limit of |x-y| >= 2**(-50), which is fine since python keeps 53 precision bits for floats, it will take at least 50 unlucky swaps before you need to reassign the orders.

    Edit: I just saw that you didn’t ask about Python specifically. Replace the precision with whatever makes sense for your system.

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

Sidebar

Related Questions

Suppose I have some objects in MongoDB: { _id:xxx, name:mike, children: [ {name:A, age:3},
I have a sorted list which is rotated and would like to do a
I have a sorted collection of objects (it can be either SortedList or SortedDictionary,
I have an array of string that I would like to have sorted based
How would I go about this, I have files which I have sorted the
I have sorted collection (List) and I need to keep it sorted at all
I need to have objects sorted by price (decimal) value for fast access. I
I have a sorted map and want to reference its ordered objects by their
I have to sort a mongodb collection in ruby via the mongodb driver.When I
I have sorted list of datetime.time and want to create intervals like. If I

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.