Three ways to sort a sequence
Python offers us three common approaches to the general problem of sorting a list
of complex items.
We can sort with the
sorted()
generator function. This will duplicate an object as part of sorting.We can sort a list with its
sort()
method and a key function. This will mutate thelist
into the requested order.We can create an intermediate sequence of objects which can be sorted easily. This is sometimes called the wrap-sort-unwrap design pattern.
In order to look at each of these in some detail, we need a collection of complex objects which we can sort. We'll use a simple dataset based on a case study in the NIST Engineering Statistics Handbook, section 7.1.6. See http://www.itl.nist.gov/div898/handbook for more information.
We've got metrics data that—after a little re-organization and cleanup—looks like this:
>>> data [['2013-09-10', '289'], ['2013-09-11', '616'], . . . , ['2013-12-07', '752'], ['2013-12-08', '739']]
We have a list-of-list structure with...