If I understand, you want to add a new item to the list with a specific sort value. If the sort value exists in the list then increment each sort value in the list where the list sort value is greater than or equal to the new item's sort value.
public static async Task Main(string[] passwordargs)
{
List<MyClassA> items = new List<MyClassA>()
; for(var i = 0; i<9; i++)
{
items.Add(new MyClassA()
{
Id = i,
Name = $"Name_{i}",
Sort = i
});
}
MyClassA newItem = new MyClassA()
{
Id = 10,
Name = "NewName",
Sort = 7
};
List<MyClassA> results = InsertMyClassA(items, newItem);
foreach(var item in results)
{
Console.WriteLine($"{item.Id}\t{item.Name}\t{item.Sort}");
}
}
public static List<MyClassA> InsertMyClassA(List<MyClassA> list, MyClassA newItem)
{
foreach(var item in list)
{
if (item.Sort >= newItem.Sort)
{
item.Sort += 1;
}
}
list.Add(newItem);
return list.OrderBy(i => i.Sort).ToList();
}
What I want to do is to create an table that has a sort column
but I do not how to set the sort column which will easily add an element between one of the list
for example.
if you want to add an element in 1 2 3 4 5 6 7 8 you have to add the other value
1 2 3 4 5 6 {the new element} 7 8
become 1 2 3 4 5 6 7 8 9
the original 7 8 will become to 8 9
but if there is many data I have to update them .
how to set the column then I do not need to update ?