''' listsorting.py -- brief samples of built-in list sorting Jeff Ondich, 30 Jan 2008 ''' # Ordinary list sorting, using the default object ordering. theList = [23, 4, -3, 19, 29] print "Unsorted list:", theList theList.sort() print "Sorted:", theList # Using the keyword parameter "reverse" with the sort method. theList = [23, 4, -3, 19, 29] theList.sort(reverse=True) print "Sorted in reverse:", theList # Using a custom comparison function with the sort method. def backwardsCmp(a, b): return cmp(b, a) theList = [23, 4, -3, 19, 29] theList.sort(backwardsCmp) print "Sorted in reverse using a custom comparison:", theList