We're back after a server migration that caused effbot.org to fall over a bit harder than expected. Expect some glitches.

list.sort

s.sort([cmp[, key[, reverse]]]) sort the items of s in place.

The list.sort and list.reverse methods modify the list in place for economy of space when sorting or reversing a large list. To remind you that they operate by side effect, they don’t return the sorted or reversed list.

The optional cmp argument specifies a custom comparison function of two arguments (list items) which should return a negative, zero or positive number depending on whether the first argument is considered smaller than, equal to, or larger than the second argument: “cmp=lambda x,y: cmp(x.lower(), y.lower())”

The optional key argument specifies a function of one argument that is used to extract a comparison key from each list element: “key=str.lower”

The optional reverse is a boolean value. If true, the list elements are sorted as if each comparison were reversed.

In general, the key and reverse conversion processes are much faster than specifying an equivalent cmp function. This is because cmp is called multiple times for each list element while key and reverse touch each element only once.

Starting with Python 2.3, the list.sort method is guaranteed to be stable. A sort is stable if it guarantees not to change the relative order of elements that compare equal — this is helpful for sorting in multiple passes (for example, sort by department, then by salary grade).

While a list is being sorted, the effect of attempting to mutate, or even inspect, the list is undefined. The C implementation of Python 2.3 and newer makes the list appear empty for the duration, and raises ValueError if it can detect that the list has been mutated during a sort.

Changed in version 2.3: Support for None as an equivalent to omitting cmp was added.

Changed in version 2.4: Support for key and reverse was added.