I have a List
and I have the new order in which the List
should have the items in int[]
I want that the item in List
should be re-ordered as per the items in int[]
. Here is my code to do that:
class Program
{
static void Main(string[] args)
{
List<Test> tests = new List<Test>() {
new Test(){ No = 201 },
new Test(){ No = 101 },
new Test(){ No = 300 },
new Test(){ No = 401 },
new Test(){ No = 500 },
new Test(){ No = 601 }
};
int[] newOrder = new int[6] { 201, 401, 300, 101, 601, 500 };
//after the opration the List should contain items in order 201, 401, 300, 101, 601, 500
List<Test> newTests = new List<Test>();
foreach(var order in newOrder)
{
var item = tests.SingleOrDefault(t => t.No == order);
if (item != null)
newTests.Add(item);
}
}
}
This works fine. But it creates a separate List
and performs operation on it. Is there better way where I can use may be built in .Net operation for this or may be perform the operation on the same List
without creating these Temp List
etc?
Thank you.