50 XP

59. Sort by age, then name

Intermediate sorting

Write a function sort_people(people) that takes a list of people, each a pair [name, age], and returns a new list of the same pairs in this order:

  • the oldest first, down to the youngest;
  • people of the same age by name, from A to Z.

Two people can share a name, or a name and an age: keep them all.

Sorting by age alone isn't enough, and neither is reversing a sort by age and name: that would put the names from Z to A as well.

Example 1

Inputpeople = [["Ann", 30], ["Bob", 25], ["Cy", 30]]
Output[["Ann", 30], ["Cy", 30], ["Bob", 25]]
ExplanationThe two 30s first, Ann before Cy.

Example 2

Inputpeople = [["Zoe", 20], ["Al", 20]]
Output[["Al", 20], ["Zoe", 20]]
ExplanationSame age, so by name: Al before Zoe.

Example 3

Inputpeople = []
Output[]
ExplanationNobody to sort.

Constraints

  • people has 0 to 50 pairs.
  • A name is 1 to 20 letters, starting with a capital; an age is a whole number from 0 to 120.

Submit also runs 6 more tests you can't see.

Practises Lambda, map and filter (lesson 19).

Code
def sort_people(people):
    """Sort people oldest first, then by name.

    people: list of [name, age] pairs (str, int)
    Returns: list of the same pairs, by age from oldest,
        same ages by name from A to Z
    """
    # Write your code here
    pass