简体   繁体   English

插入到使用对象属性进行排序的对象的C#列表中

[英]Inserting into a C# List of Objects that uses a Property of the Object to Sort

There's a list of objects, each object representing a record from a database. 有一个对象列表,每个对象代表数据库中的一条记录。 To sort the records there is a property called SortOrder. 为了对记录进行排序,有一个名为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. 我需要能够更改SortOrder并更新列表中其他对象的SortOrder。 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. 我需要知道如何将其更改多个,并在其他记录上调整SortOrder。 Any ideas? 有任何想法吗?

This could be done by first getting the original SortOrder and the updated SortOrder . 这可以通过首先获取原始SortOrder和更新后的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 . 然后,您将遍历您的集合并调整落在originalupdated之间的任何其他GroupInfo对象的SortOrder you could put all of this in a "SetSortOrder" function that takes in the containing collection. 您可以将所有这些内容放入包含集合的“ SetSortOrder”函数中。

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. 更新:按照建议,我将逻辑移到了静态帮助器函数中。

var sortedList = list.OrderBy(item => item.SortOrder);

Edit: Sorry, I misunderstood. 编辑:对不起,我误会了。 You will need to write yourself a method outside of GroupInfo to handle the updating of that property. 您将需要在GroupInfo之外编写一个方法来处理该属性的更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM