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
Recall & Review
beginner
What does the OrderBy method do in C#?
The OrderBy method sorts a collection in ascending order based on a key you specify. It returns a new sorted sequence without changing the original collection.
Click to reveal answer
beginner
How do you sort a list of numbers in descending order using LINQ?
Use OrderByDescending method. For example: numbers.OrderByDescending(n => n) sorts numbers from largest to smallest.
Click to reveal answer
intermediate
What is the difference between OrderBy and ThenBy?
OrderBy sorts the collection by the first key. ThenBy is used to sort by a second key when the first keys are equal, allowing multi-level sorting.
Click to reveal answer
beginner
Can OrderBy modify the original list?
No, OrderBy does not change the original list. It returns a new sorted sequence. To change the original list, you can use List.Sort().
Click to reveal answer
beginner
How do you sort a list of objects by a property using OrderBy?
You provide a lambda expression that selects the property. For example, people.OrderBy(p => p.Age) sorts people by their Age property in ascending order.
Click to reveal answer
Which method sorts a collection in ascending order by a key in C#?
AReverse
BOrderByDescending
CThenByDescending
DOrderBy
✗ Incorrect
OrderBy sorts in ascending order. OrderByDescending sorts in descending order.
What does ThenBy do in sorting?
ASorts by a second key when the first keys are equal
BSorts the collection by the first key
CReverses the order of the collection
DFilters the collection
✗ Incorrect
ThenBy is used for secondary sorting after OrderBy.
Does OrderBy change the original list?
AYes, it modifies the original list
BNo, it returns a new sorted sequence
COnly if you call <code>ToList()</code>
DOnly for arrays
✗ Incorrect
OrderBy returns a new sorted sequence and does not modify the original collection.
How do you sort a list of strings in descending order?
Alist.OrderByDescending(s => s)
Blist.OrderBy(s => s)
Clist.ThenBy(s => s)
Dlist.SortDescending()
✗ Incorrect
OrderByDescending sorts in descending order.
Which method would you use to sort a list of objects by their Name property?
Alist.Sort()
Blist.OrderBy(o => o.Age)
Clist.OrderBy(o => o.Name)
Dlist.Reverse()
✗ Incorrect
Use OrderBy with a lambda selecting the Name property.
Explain how to sort a list of objects by multiple properties using LINQ.
Think about how to chain sorting methods.
You got /4 concepts.
Describe the difference between OrderBy and List.Sort().
Consider whether the original list changes.
You got /4 concepts.
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".