Computer Science Grade 9 20 min

Sorting Objects

Sorting Objects

Tutorial Preview

1

Introduction & Learning Objectives

Learning Objectives Define what an 'object' is in programming and explain why sorting objects requires a specific key or attribute. Explain the role of a 'comparison key' in telling a sorting algorithm how to order objects. Use a built-in sorting function with a custom key to sort a list of objects by a specific attribute. Trace the steps of a simple sorting algorithm (e.g., Selection Sort) on a list of objects to understand the comparison process. Modify a sorting key to sort the same list of objects in both ascending and descending order. Compare the results of sorting a list of objects by different attributes (e.g., sorting students by name vs. by grade). Ever wonder how your music app can instantly sort your favorite songs by title, artist, or release...
2

Key Concepts & Vocabulary

TermDefinitionExample ObjectA data structure that groups related variables (attributes) and functions together. It represents a real-world thing, like a person, a car, or a song.A `Student` object could have attributes like `name = 'Alice'`, `student_id = 123`, and `grade = 95`. Attribute (or Property)A piece of data stored within an object. It's like a variable that belongs to the object.For a `Car` object, `color` and `speed` are attributes. For our `Student` object, `name` is an attribute. Comparison KeyThe specific attribute of an object that a sorting algorithm uses to compare two objects and decide their order.To sort a list of `Student` objects by their grade, the `grade` attribute is the comparison key. Sorting AlgorithmA step-by-step procedure for putting elements...
3

Core Syntax & Patterns

The `sort(key=...)` Pattern list_of_objects.sort(key=lambda obj: obj.attribute) To sort a list of objects, you must provide a 'key' to the built-in sort method. The key is a small function (often a `lambda`) that takes one object from the list as input and returns the value of the attribute you want to sort by. The sorting algorithm then uses these returned values to compare the objects. The `sort(reverse=True)` Pattern list_of_objects.sort(key=lambda obj: obj.attribute, reverse=True) To sort in descending (largest to smallest) order, you add the `reverse=True` argument to the sort method. The key function remains the same, specifying *what* to sort by, while `reverse=True` specifies the *direction* of the sort.

4 more steps in this tutorial

Sign up free to access the complete tutorial with worked examples and practice.

Sign Up Free to Continue

Sample Practice Questions

Challenging
You have a list of `Rectangle` objects, each with `width` and `height` attributes. You want to sort them by area (width * height) from smallest to largest, without adding a new `area` attribute to the object. Which `sort` call is correct?
A.rectangles.sort(key=lambda r: r.width * r.height)
B.rectangles.sort(key=lambda r: r.area)
C.rectangles.sort(key=area)
D.rectangles.sort(key=lambda r: r.width, r.height)
Challenging
You have a list of `Student` objects, each with a list of integer grades (e.g., `student.grades = [85, 95, 90]`). How would you sort the students by their average grade in descending order?
A.students.sort(key=lambda s: s.grades[0], reverse=True)
B.students.sort(key=lambda s: sum(s.grades) / len(s.grades), reverse=True)
C.students.sort(key=lambda s: average(s.grades), reverse=True)
D.students.sort(key=lambda s: s.grades, reverse=True)
Challenging
You need to sort `User` objects first by `level` (descending), and then for users with the same level, by `username` (ascending, A-Z). Which key correctly implements this?
A.key=lambda u: (-u.level, u.username)
B.key=lambda u: (u.level, -u.username)
C.key=lambda u: u.level + u.username
D.This requires two separate sort calls.

Want to practice and check your answers?

Sign up to access all questions with instant feedback, explanations, and progress tracking.

Start Practicing Free

More from Algorithms and Complexity

Ready to find your learning gaps?

Take a free diagnostic test and get a personalized learning plan in minutes.