I have List, and I have a new order in which the Listelements in should be int[], I want the element in to Listbe reordered according to the elements in int[]. Here is my code:
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 };
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);
}
}
}
It works great. But he creates a separate one Listand performs an operation on it. Is there a better way that I can use it that can be built into the .Net operation for this, or can perform the operation on the same Listwithout creating these Temp List, etc.?
Thank.
source
share