public async Task <List <Product> > SortProduct(string type) { ISorting sorting; List <Product> productList = await _context.Products .AsNoTracking() .ToListAsync(); switch (type) { case "name": sorting = new SortByName(); sorting.ProductSort(ref productList); break; case "price": sorting = new SortByPrice(); sorting.ProductSort(ref productList); break; case "quantity": sorting = new SortByQuantity(); sorting.ProductSort(ref productList); break; case "rating": sorting = new SortByRating(); sorting.ProductSort(ref productList); break; default: sorting = new SortByPrice(); sorting.ProductSort(ref productList); break; } return(productList); }
static void listDemo() { List <Product> lstPro = new List <Product>(); lstPro.Add(new Product { ProductID = 250, BrandName = "Dell.......", Description = "laptop........", Price = 45000.00 }); lstPro.Add(new Product { ProductID = 200, BrandName = "Dell.......", Description = "15 inch Monitor", Price = 3400.44 }); lstPro.Add(new Product { ProductID = 150, BrandName = "Microsoft..", Description = "Windows 7.....", Price = 7000.50 }); lstPro.Add(new Product { ProductID = 120, BrandName = "Seagate....", Description = "Dvd Drive.....", Price = 540.00 }); lstPro.Add(new Product { ProductID = 100, BrandName = "Logitech...", Description = "Optical Mouse", Price = 540.00 }); Console.WriteLine("Before Sorting the list is..........."); Console.WriteLine(""); foreach (Product p in lstPro) { Console.WriteLine(p.ProductID + "\t\t" + p.BrandName + "\t\t" + p.Description + "\t\t" + p.Price); } //by default by product id decreasing order lstPro.Sort(); Console.WriteLine("After default sort by product id List is......"); Console.WriteLine(""); foreach (Product p in lstPro) { Console.WriteLine(p.ProductID + "\t\t" + p.BrandName + "\t\t" + p.Description + "\t\t" + p.Price); } //menu driven sorting...... //Option for sorting based on Brand name or price based on input provided at runtime. //If two products contain same brand name, description should be considered. //Similarly if products have same price, product id’s should be considered while displaying them in order Console.WriteLine("Choose one of these........\n1.Sorting based on Brand name" + "\n2.Sorting based on Description\n3.Sorting based on Price"); int choice = int.Parse(Console.ReadLine()); if (choice == 1) { Console.WriteLine("Sort by BrandName....."); SortByBrand sort_by_brand = new SortByBrand(); lstPro.Sort(sort_by_brand); } else if (choice == 2) { Console.WriteLine("Sort by Description....."); SortByDescription sbd = new SortByDescription(); lstPro.Sort(sbd); } else if (choice == 3) { Console.WriteLine("Sort by Price......"); SortByPrice sbp = new SortByPrice(); lstPro.Sort(sbp); } else { Console.WriteLine("Wrong Choice Entered......"); } Console.WriteLine("List values are..........\n"); Console.WriteLine(""); foreach (Product p in lstPro) { Console.WriteLine(p.ProductID + "\t\t" + p.BrandName + "\t\t" + p.Description + "\t\t" + p.Price); } }