在C#中更新序列后重新排列通用列表

时间:2019-06-08 02:28:49

标签: c# .net list linq sorting

假设我有一个通用列表,例如:

var categories = new List<Category>() {
    new Category() { sequence = 3, categoryName = "Category F" },
    new Category() { sequence = 1, categoryName = "Category S" },
    new Category() { sequence = 2, categoryName = "Category Z" },
    new Category() { sequence = 4, categoryName = "Category X" },
    new Category() { sequence = 5, categoryName = "Category V" }
};

我正在尝试将给定的序列号替换为另一个序列号2的列表中的更新项,序列号为2,编号为5,如下所示:

int currSeq = 2;
int newSeq = 5;
var item = categories.Find(a => a.sequence == currSeq);
item.sequence = newSeq;

如果newSeq没有重复,例如newSeq = 6,我可以执行OrderBy:

categories = categories.OrderBy(c => c.sequence).ToList();

但是问题是当它们成为重复项时,我需要将newSeq变成序列5中的对象,并碰撞其他重复的序列5 + 1。

请协助解决此问题的任何指导。谢谢。

更新:在某些情况下,当新序列<旧序列时,输出不符合要求。

方案A:

var categories = new List<Category>() {
    new Category() { sequence = 3, categoryName = "Category F" },
    new Category() { sequence = 1, categoryName = "Category S" },
    new Category() { sequence = 2, categoryName = "Category Z" },
    new Category() { sequence = 5, categoryName = "Category X" },
    new Category() { sequence = 6, categoryName = "Category V" }
};
int currSeq = 2;
int newSeq = 1;
var result = categories.UpdateSequence(currSeq,newSeq).OrderBy(x=>x.sequence);

输出方案A:

1: Category S
3: Category Z
4: Category F
5: Category X
6: Category V

所需的输出方案A:

1: Category Z
2: Category S
3: Category F
5: Category X
6: Category V

场景B:

int currSeq = 3;
int newSeq = 1;
var result = categories.UpdateSequence(currSeq,newSeq).OrderBy(x=>x.sequence);

输出方案B:

1: Category Z
2: Category S
4: Category F
5: Category X
6: Category V

所需的输出方案B:

1: Category F
2: Category S
3: Category Z
5: Category X
6: Category V

4 个答案:

答案 0 :(得分:1)

根据评论和修改的OP更新。

假设您的Category类定义如下。

public class Category
{
    public int sequence{get;set;}
    public string categoryName{get;set;}
}

当newValue

 public static List<Category> UpdateSequence(this List<Category> categories, int oldValue,int newValue)
 {
        var invalidState = -1;
        if(oldValue>newValue)
        {
            categories.Single(x=>x.sequence == oldValue).sequence = invalidState;
            if(categories.Any(x=>x.sequence == newValue))
            {
                categories = UpdateSequence(categories, newValue,newValue+1);
            }
            categories.Single(x=>x.sequence == invalidState).sequence = newValue;
        }
        else
        {
            if(categories.Any(x=>x.sequence == newValue))
            {
                categories = UpdateSequence(categories, newValue,newValue+1);
            }
            categories.Single(x=>x.sequence==oldValue).sequence = newValue;
        }
        return categories;
    }

输入

var categories = new List<Category>() {
    new Category() { sequence = 3, categoryName = "Category F" },
    new Category() { sequence = 1, categoryName = "Category S" },
    new Category() { sequence = 2, categoryName = "Category Z" },
    new Category() { sequence = 5, categoryName = "Category X" },
    new Category() { sequence = 6, categoryName = "Category V" }

场景1:

int currSeq = 2;
int newSeq = 1;
var result = categories.UpdateSequence(currSeq,newSeq).OrderBy(x=>x.sequence);

输出

1 Category Z 
2 Category S 
3 Category F 
5 Category X 
6 Category V 

场景2

int currSeq = 3;
int newSeq = 1;
var result = categories.UpdateSequence(currSeq,newSeq).OrderBy(x=>x.sequence);

输出

1 Category F 
2 Category S 
3 Category Z 
5 Category X 
6 Category V 

答案 1 :(得分:0)

按照elgonzo的评论说。加号以间隔较大的序列号开始,即1000、2000、3000,而不是1,2,3。我不知道您的用例,但是除非您要重新订购很多,否则这将最大限度地减少您必须要做的序列号“颠簸”数量。

答案 2 :(得分:0)

假设您已经基于Sequence对列表进行了排序,则可以使用LINQ的FindIndex方法使用currSeqnewSeq查找元素的索引然后使用RemoveAtInsert将更新后的元素放置在正确的位置。

类似这样的东西:

int currSeq = 2;
int newSeq = 5;
var oldIndex = categories.FindIndex(i => i.Sequence == currSeq);
var newIndex = categories.FindIndex(i => i.Sequence == newSeq);

if(oldIndex > -1)
{
    var oldItem = lst[oldIndex];
    oldItem.ID = newSeq;

    if(newIndex > -1)
    {
        newIndex = newIndex-1 < 0 ? 0 : newIndex - 1;    //in case the first element is the existing one
        categories.RemoveAt(oldIndex);
        categories.Insert(newIndex, oldItem);
    }
    else
    {
        categories = lst.OrderBy(i => i.ID).ToList();
    }
}

答案 3 :(得分:0)

我认为这可以解决您的问题。但是在插入时,以下所有行也会移动。这样可以吗?

    var categories = new List<Category>() {
        new Category() { sequence = 3, categoryName = "Category F" },
        new Category() { sequence = 1, categoryName = "Category S" },
        new Category() { sequence = 2, categoryName = "Category Z" },
        new Category() { sequence = 4, categoryName = "Category X" },
        new Category() { sequence = 5, categoryName = "Category V" }
    };

    List<string> list = new List<string>();
    for (int i = 0; i < 6; i++)
    {
        list.Add("");
    }

    foreach (var item in categories)
    {
        list[item.sequence] = item.categoryName;
    }

    var removeFrom = 2;
    var removeTo = 5;

    list.Insert(removeTo, list[removeFrom]);
    list.RemoveAt(removeFrom);