简体   繁体   中英

change and update order of generic list items with C#

I am working on an algorithm that answered here which solve specific problems, but I want to make this algorithm generic to use it for any type of lists that I have in my application. but I am stuck in.

for example: I have a Scenario class which contains a variable called Order:

public class Scenario
{
 public int Id{get; set;}; // not incremented can be any value!
 public int Order{get; set;};
 public string Title{get; set;};
}

and I can call this method to change the order of any scenario as I want:

private void UpdateOrders(Scenario targetScenario, short newOrder, List<Scenario> items)
{
        items = items.OrderBy(_ => _.Order).ToList();
        bool seen = false;
        for (int i = 0; i < items.Count; i++)
        {
            if (items[i].Id == targetScenario.Id)
            {
                items[i].Order = newOrder;
                seen = true;
            }
            else if (seen)
            {
                if (items[i].Order <= newOrder)
                {
                    items[i].Order--; // move it left
                }
            }
            else
            {
                if (items[i].Order >= newOrder)
                {
                    items[i].Order++; // move it right
                }
            }
        }
}

and this works for any other specific lists, but this algorithm must be repeated each time to update order of each list. so I try to extend this algorithm to avoid writing duplicate code. here what I try but obviously its not working:

private void UpdateOrders<T>(T targetScenario, short newOrder, List<T> items)
    {
        items = items.OrderBy(_ => _.Order).ToList();
        bool seen = false;
        for (int i = 0; i < items.Count; i++)
        {
            if (items[i].Id == targetScenario.Id)
            {
                items[i].Order = newOrder;
                seen = true;
            }
            else if (seen)
            {
                if (items[i].Order <= newOrder)
                {
                    items[i].Order--; // move it left
                }
            }
            else
            {
                if (items[i].Order >= newOrder)
                {
                    items[i].Order++; // move it right
                }
            }
        }
    }

Can anyone help?

Thanks

You need to make a contract where you can ensure the items you use have a property of int Order . Then restrict your function T type to be of your contract type.

public interface ISortable
{
    int Id { get; set; }
    int Order { get; set; }
}

public class Scenario : ISortable
{
    public int Id { get; set; }
    public int Order { get; set; }
    public string Title { get; set; }
}


private void UpdateOrders<T>(T targetScenario, short newOrder, List<T> items) where T : ISortable
{
    items = items.OrderBy(_ => _.Order).ToList();
    bool seen = false;
    for (int i = 0; i < items.Count; i++)
    {
        if (items[i].Id == targetScenario.Id)
        {
            items[i].Order = newOrder;
            seen = true;
        }
        else if (seen)
        {
            if (items[i].Order <= newOrder)
            {
                items[i].Order--; // move it left
            }
        }
        else
        {
            if (items[i].Order >= newOrder)
            {
                items[i].Order++; // move it right
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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