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

OrderBy and sorting in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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
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
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
How do you sort a list of strings in descending order?
Alist.OrderByDescending(s =&gt; s)
Blist.OrderBy(s =&gt; s)
Clist.ThenBy(s =&gt; s)
Dlist.SortDescending()
Which method would you use to sort a list of objects by their Name property?
Alist.Sort()
Blist.OrderBy(o =&gt; o.Age)
Clist.OrderBy(o =&gt; o.Name)
Dlist.Reverse()
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.