0
0
C Sharp (C#)programming~30 mins

OrderBy and sorting in C Sharp (C#) - Mini Project: Build & Apply

Choose your learning style9 modes available
Sorting a List of Products by Price
📖 Scenario: You work in a store and have a list of products with their prices. You want to show the products sorted by their price from lowest to highest.
🎯 Goal: Build a small program that creates a list of products with prices, then sorts them by price using OrderBy, and finally prints the sorted list.
📋 What You'll Learn
Create a list of products with exact names and prices
Create a variable to hold the sorted products using OrderBy
Use a foreach loop to print each product name and price
💡 Why This Matters
🌍 Real World
Sorting products by price helps customers find cheaper or more expensive items easily in online stores or inventory systems.
💼 Career
Sorting and ordering data is a common task in software development, especially in e-commerce, data analysis, and user interface design.
Progress0 / 4 steps
1
Create the list of products
Create a list called products of type List<KeyValuePair<string, double>> with these exact entries: ("Apple", 1.20), ("Banana", 0.50), ("Cherry", 2.00), ("Date", 3.00), ("Elderberry", 1.50).
C Sharp (C#)
Need a hint?

Use new List<KeyValuePair<string, double>> { ... } and add each product with new KeyValuePair<string, double>(name, price).

2
Create a sorted list using OrderBy
Create a variable called sortedProducts that stores the products sorted by price using products.OrderBy(p => p.Value). Add using System.Linq; at the top if needed.
C Sharp (C#)
Need a hint?

Use var sortedProducts = products.OrderBy(p => p.Value); to sort by the price.

3
Print the sorted products
Use a foreach loop with variables product to iterate over sortedProducts. Inside the loop, print the product name and price using Console.WriteLine($"{product.Key}: {product.Value}").
C Sharp (C#)
Need a hint?

Use foreach (var product in sortedProducts) and print with Console.WriteLine($"{product.Key}: {product.Value}").

4
Run and see the sorted output
Run the program and observe the output. It should print the products sorted by price from lowest to highest.
C Sharp (C#)
Need a hint?

The output should list products sorted by price from lowest to highest.