public class Product { public string Code { get; private set; } public Product(string code) { Code = code; } } List<Product> sourceProductsOrder = new List<Product>() { new Product("BBB"), new Product("QQQ"), new Product("FFF"), new Product("HHH"), new Product("PPP"), new Product("ZZZ")}; List<Product> products = new List<Product>() { new Product("ZZZ"), new Product("BBB"), new Product("HHH")}; I have two product lists and I want to reorder the second one with the same order as the first. How can I reorder the products list so that the result would be : "BBB", "HHH", "ZZZ"?
EDIT: Changed Code property to public as @juharr mentioned
Product("BBB")object, or is it the same object shared between the two lists?Codepublic, or override theEquals(andGetHashCode) method ofProductto compare theCodevalue.Codeis currently a public field, not a property, which is generally considered a bad practice. You can make it an auto property like thispublic string Code { get; private set; }. This also makes the object immutable which you may or may not find desirable.