Oldest First

Emma wants to see the register in order, from the oldest animal to the youngest, and in neat columns. Below the list, print the line Oldest first:, and below it, every animal with its name, its kind and its age:

Pearl the hen, age 1
Oldest first:
Bella   cow   5
Zoé     cat   4
Rex     dog   3
Goldie  hen   2
Pearl   hen   1

Two animals born in the same year are in the alphabetical order of their names. The names are in a column 8 characters wide, and the kinds in a column 6 characters wide. Count the characters, not the bytes, so that Zoé's row lines up too.

A tip: table.sort can sort the animals without a comparison function when their class tells it which of two animals comes first, as it did for the bags in the lesson on objects. For the columns, look back at the exercise on letters with accents.

Instructions

Print Oldest first:, and below it the animals from the oldest to the youngest, each with its name, kind and age.

Line up the columns: the names 8 characters wide, and the kinds 6 characters wide.

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

2/3