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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:34:31+00:00 2026-05-19T00:34:31+00:00

In a web application I have a set of entities in a database with

  • 0

In a web application I have a set of entities in a database with a numeric field responsible for their ordering. On a client side these entities are displayed as a sortable list allowing users to change their order.

As of now I have two solutions for managing order updates and none of them satisfies me.

The first one is simple: every time user changes some item order, traverse updated list, get all items IDs into array, send it to a server and issue a series of updates where each item order is it’s ID’s index in array.
Drawbacks of this approach: lots of unnecessary updates, inability to correctly handle a situation when items array sent to a server does not contain all the entities IDs.

The second one is as follows: when user changes an item order, changed item’s ID is sent to a server along with ID’s of items that “surround” changed item in it’s new place in list. On server item’s new order is calculated by (previous.order + next.order) / 2. So if item with order 3 gets moved between items with orders 5 and 6, it’s new order becomes 5.5
This solution requires only one update per change but also has a serious problem: due to the algorithm used every change increases the decimal part in order numbers and sooner or later it requires more precision then my database can provide (I use MongoDB for this case but I suppose it’s not that important).

My question is if any other more efficient and correct ways exist or maybe my current approaches can be somehow improved?

  • 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-05-19T00:34:32+00:00Added an answer on May 19, 2026 at 12:34 am

    Describe the order in the database with an ordinal number, which starts at 0 for the first item and increases by 1 for every subsequent item. Then, you just need to send the ordinals of the moved item, and the ordinal of its new previous neighbour. You then do (i’m using $ to mark variables – you’ll need to pass these into the queries from your code):

    -- if $moved > $previous, and it's moving backwards, move everything between the new previous neighbour and the item forward one
    update items
    set ordinal = ordinal + 1
    where ordinal > $previous
    and ordinal < $moved;
    
    -- else $moved < $previous, and it's moving forwards, move everything between the item and the new previous neighbour backwards one
    update items
    set ordinal = ordinal - 1
    where ordinal > $moved
    and ordinal <= $previous;
    
    -- then move the item
    update items
    set ordinal = $previous + 1
    where ordinal = $moved;
    

    You could add a unique constraint to the ordinal column to help ensure integrity, but you’d have to be a bit cleverer about how you did updates.

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

Sidebar

Related Questions

I have a Dynamic Data Entities Web Application that uses a database with GUIDs
In my web application we have many users.I want to set permission for each
I have a web application that has one set of files used by 50+
We have an ASP.net 2.0 web application which is using themes set on the
In my web application I have set the session timeout for 30 minutes. But
I am creating a web application that manages documents. These documents have stages. Users
I have an ASP.NET MVC3 web application with UI, Business (entities), and Data (DbContext)
I have a Flask web application using SQLAlchemy with MySQL, and I have set
As part of our web application's build process, I have set up our XSLT
I'm writing my SPring MVC web application. I have set my session time out

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.