Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Use sorted with multiple sort criteria

Sort a list of people by last name, then first name, and then age (descending).

Python practice16 minFunctions as Objects (Lambda, Map/Filter)IntermediateLast updated March 20, 2026

Problem statement

Write a function sort_people(people) that accepts a list of 3-tuples (first_name, last_name, age) and returns a new list sorted by: 1) last name (ascending), 2) first name (ascending), and 3) age (descending). Use sorted with a single key function (lambda returning a tuple).

Task

Practice using sorted with a lambda key that returns a tuple to express multiple sort criteria, including reversing a criterion by inverting the value.

Examples

Sort by last then first

Input

sort_people([('John','Doe',30),('Jane','Doe',25)])

Output

[('Jane', 'Doe', 25), ('John', 'Doe', 30)]

Same last name 'Doe' so sorted by first name ascending.

Input format

A Python list of tuples where each tuple is (first_name: str, last_name: str, age: int).

Output format

A new list of tuples sorted according to the described multi-criteria.

Constraints

Do not use multiple sorts in sequence. Use a single sorted(...) call with a lambda key returning a tuple. Do not modify the input list.

Samples

Sample 1

Input

sort_people([('Sam','Smith',20),('Sam','Smith',30)])

Output

[('Sam', 'Smith', 30), ('Sam', 'Smith', 20)]

Same first and last names, so age is used descending.