Bird
Raised Fist0

Given a list of students with properties Name and Score, how do you sort the list first by Score descending, then by Name ascending using LINQ?

hard🚀 Application Q15 of Q15
C Sharp (C#) - LINQ Fundamentals
Given a list of students with properties Name and Score, how do you sort the list first by Score descending, then by Name ascending using LINQ?
Astudents.OrderByDescending(s => s.Score).ThenBy(s => s.Name);
Bstudents.OrderBy(s => s.Score).OrderBy(s => s.Name);
Cstudents.OrderBy(s => s.Name).OrderByDescending(s => s.Score);
Dstudents.OrderByDescending(s => s.Name).ThenByDescending(s => s.Score);
Step-by-Step Solution
Solution:
  1. Step 1: Understand multi-level sorting

    To sort by multiple keys, use OrderBy or OrderByDescending for the first key, then ThenBy or ThenByDescending for the next keys.
  2. Step 2: Apply correct order and directions

    We want to sort by Score descending first, then by Name ascending, so use OrderByDescending(s => s.Score) followed by ThenBy(s => s.Name).
  3. Final Answer:

    students.OrderByDescending(s => s.Score).ThenBy(s => s.Name); -> Option A
  4. Quick Check:

    OrderByDescending + ThenBy for multi-level sort [OK]
Quick Trick: Use ThenBy after OrderBy for secondary sorting [OK]
Common Mistakes:
MISTAKES
  • Using multiple OrderBy calls instead of ThenBy
  • Mixing ascending and descending incorrectly
  • Sorting by wrong property order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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