Reputation: 16040
I would like to print products in order of quantity.The product with a bigger total should be first.
What am I missing here as it's NOT printing in order or total
class Program
{
static void Main()
{
var products=new List<Product>
{
new Product {Name = "Apple", Total = 5},
new Product {Name = "Pear", Total = 10}
};
var productsByGreatestQuantity = products.OrderBy(x => x.Total);
foreach (var product in productsByGreatestQuantity)
{
System.Console.WriteLine(product.Name);
}
System.Console.Read();
}
}
public class Product
{
public string Name { get; set; }
public int Total { get; set; }
}
Upvotes: 5
Views: 376
Reputation: 27214
Change:
var productsByGreatestQuantity = products.OrderBy(x => x.Total);
to:
var productsByGreatestQuantity = products.OrderByDescending(x => x.Total);
Upvotes: 2