Bird
0
0

You have a list of employees with properties Name and Age. How do you sort the list first by Age ascending, then by Name alphabetically?

hard🚀 Application Q8 of 15
C Sharp (C#) - LINQ Fundamentals
You have a list of employees with properties Name and Age. How do you sort the list first by Age ascending, then by Name alphabetically?
Aemployees.Sort(e => e.Age, e => e.Name);
Bemployees.OrderBy(e => e.Name).ThenBy(e => e.Age);
Cemployees.OrderByDescending(e => e.Age).ThenByDescending(e => e.Name);
Demployees.OrderBy(e => e.Age).ThenBy(e => e.Name);
Step-by-Step Solution
Solution:
  1. Step 1: Identify primary and secondary sorting

    Primary sort is by Age ascending, secondary by Name ascending.
  2. Step 2: Use OrderBy and ThenBy

    OrderBy sorts by primary key, ThenBy sorts by secondary key.
  3. Final Answer:

    employees.OrderBy(e => e.Age).ThenBy(e => e.Name); -> Option D
  4. Quick Check:

    OrderBy then ThenBy for multi-level sort [OK]
Quick Trick: Use OrderBy then ThenBy for multi-level sorting [OK]
Common Mistakes:
MISTAKES
  • Reversing order of keys
  • Using OrderByDescending instead of OrderBy
  • Trying to use Sort with multiple lambdas

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes