简体   繁体   中英

Grouping and ordering class object based on there certain properties?

I have product class which has Description property and have 3 product objects:

Product product1 = new Product("This is bottom product");
Product product2 = new Product("This is top product");
Product product3 = new Product("This is medium product");

List<Product> lst = new List<Product>();
lst.Add(product1);
lst.Add(product2);
lst.Add(product3);

I want to re-arrange these objects so that all products with description of top comes on top and product with description as bottom comes at bottom.

var res = lst.Where(p => p.Desription == "This is top product")
                         .Concat(lst.Where(p => p.Desription == "This is medium product"))
                         .Concat(lst.Where(p => p.Desription == "This is bottom product")).ToList();

I have coded the above query to achieve this. This returns me correct results. But I am not sure if this is most efficient way to solve this problem?

Can someone suggest any alternative/performance-wise-better approach?

Note: I have simplified the problem here. Otherwise product has many more objects and properties.

Edited:

The better solution than comparer(quicker):

 public enum ProductType
    {
        Bottom=0,
        Medium,
        Top,
    }

    public class Product
    {
        public ProductType Type { get; set; }
        //the rest of properties here
    }

 var list = new List<Product>();
 var orderedList = list.OrderBy(x => (int)x.Type).ToList();

If you don't want to add anything to the existing class, maybe wrap it or extend?

Given this in your question:

var res = lst.Where(p => p.Desription == "This is top product")
                         .Concat(lst.Where(p => p.Desription == "This is medium product"))
                         .Concat(lst.Where(p => p.Desription == "This is bottom product")).ToList();

I believe what you are looking for is something like this:

var res = lst.OrderBy(p => p.Desription);

I'd like to point out that your variable for "Description" is misspelled.

Use a helper Dictionary :

Dictionary<string, int> myDic = new Dictionary<string, int>
{
    {"top", 1},
    {"medium", 2},
    {"bottom", 3}
};
var res = lst.OrderBy(c => myDic[c.Desription.Split(' ')[2]]);

Try something like this, if you know each description will only contain the specific wording 'top', 'bottom', 'medium' and only one of those:

List<Product> res = new List<Product>();
for (int i = 0; i < lst.Count(); i++)
{
    res.AddRange(lst.Where(p => p.Desription.Contains("top").ToList());
    res.AddRange(lst.Where(p => p.Desription.Contains("medium").ToList());
    res.AddRange(lst.Where(p => p.Desription.Contains("bottom").ToList());
}

I use AddRange instead of Add since there may be multiple 'top', 'medium' or 'bottom' products.

Also it won't work if the descriptions contain 'top' and 'bottom' in the same sentence, 'top' and 'medium', etc.

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