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#?
✗ Incorrect
OrderBy sorts in ascending order. OrderByDescending sorts in descending order.What does
ThenBy do in sorting?✗ Incorrect
ThenBy is used for secondary sorting after OrderBy.Does
OrderBy change the original list?✗ 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?
✗ Incorrect
OrderByDescending sorts in descending order.Which method would you use to sort a list of objects by their
Name property?✗ 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.