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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:16:35+00:00 2026-05-23T10:16:35+00:00

Suppose we have a simple database containing following data: name apple pear banana grape

  • 0

Suppose we have a simple database containing following data:

        name
        apple
        pear
        banana
        grape

User want to sort those fruits by the name, and we will have, with no surprise

         apple
         banana
         grape
         pear

However, for some reason, user would like to place pear as the 3rd fruit ,that means he would like to have :

         apple
         banana
         pear
         grape

And, importantly, the user want to preserve this order when he want to sort the fruits by name thereafter.

How should we tackle this problem? Of top of my head, we could add a field user_sort_id, which will be updated when user sort and manipulate the sort result and we will use that filed as the sort key.

                       init value       -> sort by name         ->place pear as the seconds
        name           user_sort_id 
        apple            0                     0                          0
        pear             1                     3                          2  
        banana           2                     1                          1  
        grape            3                     2                          3 

This approach should work in theory. However, in practice, I can not think of an elegant and fast SQL statement that could accomplish this. Any ideas or alternatives?

  • 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-23T10:16:36+00:00Added an answer on May 23, 2026 at 10:16 am

    If you want each user to have independent sort orders, you need another table.

    CREATE TABLE user_sort_order (
        name VARCHAR(?) NOT NULL REFERENCES your-other-table (name),
        user_id INTEGER NOT NULL REFERENCES users (user_id),
        sort_order INTEGER NOT NULL                -- Could be float or decimal
    );
    

    Then ordering is easy.

    SELECT name 
    FROM user_sort_order
    WHERE user_id = ?
    ORDER BY sort_order
    

    There’s no magic bullet for updating.

    • Delete all the user’s rows, and insert rows with the new order. (Brute force always works.)
    • Update every row with the new order. (Could be a lot of UPDATE statements.)
    • Track the changes in your app, and update only the changed rows and the rows that have to be “bumped” by the changes. (Parsimonious, but error-prone.)
    • Don’t let users impose their own sort order. (Usually not as bad an idea as it sounds.)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have some application A with a database. Now I want to add
Let's suppose I have a the following simplified example database consisting of three tables:
Suppose I have to develop a simple data model in Java for Order ,
Suppose I have a simple XHTML document that uses a custom namespace for attributes:
I have a simple question. Is there a way ( using reflections I suppose
Suppose I have the following CSS rule in my page: body { font-family: Calibri,
Suppose I have a table and an index on it original simple table A
I have a simple ADO.NET Entity Framework 4.0 model (edmx) which defines database tables
I have a simple question that keeps me up all night :( . Suppose
I currently have a simple database program that reads keys in from a text

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.