Python has what is probably the most elegant way of sorting a collection of objects by their attribute values: 1 sorted(people, key=attrgetter("age", "name")) Let’s break it down. sorted() We start with the sorted() function. It returns a new sorted list from the items in its first argument. 1 2 >>> sorted([3, 1, 2]) [1, 2, 3] sorted() also takes an optional key function. key, when provided, is used to extract a comparison key from the items being sorted.