Rearrange a list based on given order in c#

后端 未结 2 1335
栀梦
栀梦 2021-02-07 20:15

I have a list as follows:

{CT, MA, VA, NY}

I submit this list to a function and I get the optimum waypoint order list

{2,0,1,3}         


        
相关标签:
2条回答
  • 2021-02-07 20:43

    This seems like the simplest approach:

    oldItems = LoadItems(); //{"CT","MA","VA","NY"};
    List<string> newItems = List<string>();
    foreach(int idx in returnedIndexes)
    {
       newItems.Add(oldItems[idx]);
    }
    
    0 讨论(0)
  • 2021-02-07 20:54

    You could try the following:

    var list = new List<string>{"CT", "MA", "VA", "NY"};
    var order = new List<int>{2, 0, 1, 3};
    var result = order.Select(i => list[i]).ToList();
    
    0 讨论(0)
提交回复
热议问题