Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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#)
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#)
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#)
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#)
Hint
The output should list products sorted by price from lowest to highest.
Practice
(1/5)
1. What does the OrderBy method do in C#?
easy
A. Sorts a collection in ascending order based on a key
B. Deletes elements from a list
C. Reverses the order of elements in a list
D. Filters elements based on a condition
Solution
Step 1: Understand the purpose of OrderBy
The OrderBy method sorts elements in a collection based on a key in ascending order.
Step 2: Compare with other options
Options B, C, and D describe different operations (deletion, reversing, filtering) which OrderBy does not perform.
Final Answer:
Sorts a collection in ascending order based on a key -> Option A
Quick Check:
OrderBy = Sort ascending [OK]
Hint: OrderBy sorts ascending by key, not filtering or deleting [OK]
Common Mistakes:
Confusing OrderBy with filtering methods like Where
Thinking OrderBy modifies the original list
Mixing up OrderBy with reversing or deleting
2. Which of the following is the correct syntax to sort a list of integers named numbers in ascending order using OrderBy?
easy
A. numbers.OrderBy();
B. numbers.OrderBy(n);
C. numbers.OrderBy(n => n);
D. numbers.OrderBy(n => );
Solution
Step 1: Check the correct lambda syntax
OrderBy requires a key selector function like n => n to specify sorting key.
Step 2: Validate each option
numbers.OrderBy(n => n); uses correct lambda syntax. numbers.OrderBy(); misses the key selector. numbers.OrderBy(n); passes a variable, not a lambda. numbers.OrderBy(n => ); has incomplete lambda syntax.
Final Answer:
numbers.OrderBy(n => n); -> Option C
Quick Check:
OrderBy needs a key selector lambda [OK]
Hint: OrderBy always needs a key selector lambda like n => n [OK]
Common Mistakes:
Omitting the lambda expression inside OrderBy
Passing a variable instead of a lambda
Using incomplete or invalid lambda syntax
3. What will be the output of the following code?
var fruits = new List<string> { "banana", "apple", "cherry" };
var sorted = fruits.OrderBy(f => f);
foreach(var fruit in sorted) {
Console.Write(fruit + " ");
}
medium
A. banana apple cherry
B. apple banana cherry
C. cherry banana apple
D. apple cherry banana
Solution
Step 1: Understand the sorting key
The code sorts the list of fruits alphabetically by their string value.
Step 2: Determine the sorted order
Alphabetically, "apple" comes before "banana", which comes before "cherry".