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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:35:46+00:00 2026-06-06T13:35:46+00:00

There’s a list of objects, each object representing a record from a database. To

  • 0

There’s a list of objects, each object representing a record from a database. To sort the records there is a property called SortOrder. Here’s a sample object:

public class GroupInfo
{
    public int Id { get; set; }
    public string Text { get; set; }
    public int SortOrder { get; set; }

        public GroupInfo()
        {
            Id = 0;
            Text = string.Empty;
            SortOrder = 1;
        }
}

A list object would look like this:

var list = new List<GroupInfo>();

I need to be able to change the SortOrder and update the SortOrder on the other objects in the list. I figured out how to sort up or down by one. I need to know how to change it by more than one and adjust the SortOrder on the other records. Any ideas?

  • 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-06T13:35:49+00:00Added an answer on June 6, 2026 at 1:35 pm

    This could be done by first getting the original SortOrder and the updated SortOrder. You would then iterate through your collection and adjust the SortOrder of any other GroupInfo objects that fall inside the range between original and updated. you could put all of this in a “SetSortOrder” function that takes in the containing collection.

    public static void SetSortOrder(List<GroupInfo> groupInfos, GroupInfo target, int newSortOrder)
    {
        if (newSortOrder == target.SortOrder)
        {
            return; // No change
        }
        // If newSortOrder > SortOrder, shift all GroupInfos in that range down
        // Otherwise, shift them up
        int sortOrderAdjustment = (newSortOrder > target.SortOrder ? -1 : 1);
        // Get the range of SortOrders that must be updated
        int bottom = Math.Min(newSortOrder, target.SortOrder);
        int top = Math.Max(newSortOrder, target.SortOrder);
        // Get the GroupInfos that fall within our range
        var groupInfosToUpdate = from g in groupInfos
                                    where g.Id != target.Id
                                    && g.SortOrder >= bottom
                                    && g.SortOrder <= top
                                    select g;
        // Do the updates
        foreach (GroupInfo g in groupInfosToUpdate)
        {
            g.SortOrder += sortOrderAdjustment;
        }
    
        target.SortOrder = newSortOrder;
        // Uncomment this if you want the list to resort every time you update
        // one of its members (not a good idea if you're doing bulk changes)
        //groupInfos.Sort((info1, info2) => info1.SortOrder.CompareTo(info2.SortOrder));
    }
    

    Update: As suggested, I moved the logic into a static helper function.

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

Sidebar

Related Questions

There is a directed graph having a single designated node called root from which
There are two intents on the receiver side which are called from the same
There seem to be very different opinions about using transactions for reading from a
There's always skepticism from non-programmers when honest developers learn the techniques of black hat
There's a dropdown displaying 1-10 from where the user selects the number of textboxes
There will be 500+ threads concurrently uploading an unique object to a bucket all
There is a website called Gild.com that has different coding puzzles/challenges for users to
There are many tutorials that talk about deleting index.php from the url. But I
There is a constant I'm using from the iOS SDK. I printed out the
There's a Rails 3.2.3 web application which doesn't use any database. But in spite

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.