Editor

Fix the Bug: Case-Sensitive Sorting

Intermediate · Fix the bug · from Python Sorting: sort(), sorted(), Key Functions, and Custom Order

From the lesson: Python Sorting: sort(), sorted(), Key Functions, and Custom Order

Imagine your bookshelf at home. Books are scattered everywhere - some by title, some by color, some shoved in sideways. Finding anything takes forever.

Read the lesson

The code below tries to sort names alphabetically, but uppercase and lowercase names are mixing incorrectly. Fix the sort so it works in true alphabetical order regardless of case.

The output should be: ['alice', 'Bob', 'Charlie', 'dave', 'Eve']

names = ['Charlie', 'alice', 'Eve', 'dave', 'Bob']
result = sorted(names)
print(result)

More Sorting exercises

All Python exercises · 50 practice problems · Data Structures quiz