Skip to content
Search lessons, topics, tests…
Esc

    ↑ ↓ moveEnter openEsc close

    Coding problem

    Sort students by marks, then name

    EasyAbout 15 minFreeLearn it: OrderBy Under the Hood: O(n log n), an O(n) Buffer, Stability and Top-k

    The task

    A results page lists students from the highest mark down, and students with the same mark appear in name order.

    Complete SortStudents(List<Student> students). Student is a record with a Name and Marks. Return a new list sorted by Marks, highest first; students with the same marks are sorted by Name in ascending ordinal order, which compares characters by their numeric values. Do not change the input list. An empty list gives an empty list. The list and its students are never null, and names hold only lowercase English letters.

    Signature

    public record Student(string Name, int Marks);
    public static List<Student> SortStudents(List<Student> students)

    Examples

    • [("amy", 80), ("zoe", 95), ("ben", 80)] → [("zoe", 95), ("amy", 80), ("ben", 80)]. zoe has the highest mark, so she comes first even though her name sorts last; amy and ben tie on 80, so their names decide.
    • [] → []. An empty list stays empty.

    Limits: Up to 200,000 students.

    The tests check: an empty list, one student, higher marks first, equal marks sorted by name, several groups of equal marks, a short name before a longer one that starts the same way, the same name with different marks, duplicate students, that the input list is not changed, that the result is a new list, and 200,000 students in time.

    Hints

      The solution opens by itself when all tests pass. You remember more if you try first.

      Sign in to see the solution

      Your code

      C#Solution.csLoading the editor…

      Already included: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks;

      public record Student(string Name, int Marks);
      
      public static class Solution
      {
          // Returns a new list sorted by marks descending, then name ascending.
          public static List<Student> SortStudents(List<Student> students)
          {
              throw new NotImplementedException();
          }
      }

      Sign in to run and submit your code. It stays here while you do.