Editor

Sort the Class List

#586 · Sorting and searching · Intermediate · Review the AI's code · 60 XP · about 7 min

Someone asked an AI assistant: "Write a function that sorts students by grade, highest first. Students with the same grade should be in alphabetical order." This is its answer. It looks right, but it has a bug.

sort_students(students) takes a list of (name, grade) tuples and returns a new list sorted by grade from highest to lowest. Students with the same grade appear in alphabetical order of their names, A to Z. The input list is not changed.

Examples

sort_students([('ben', 70), ('ana', 85), ('cy', 92)])
# returns [('cy', 92), ('ana', 85), ('ben', 70)]
sort_students([('zoe', 80), ('amir', 80)])
# returns [('amir', 80), ('zoe', 80)]

Constraints

  • students has 0 to 1,000 tuples; names are lowercase, grades are whole numbers from 0 to 100.

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.