Backup of David's Livejournal

Python Sorting


Python's sorting is extremely powerful and intuitive, so it seems foolish for me to post this note-to-self here, but I want to do so, in case I forget the operator module.

import operator
rows.sort(key=operator.itemgetter(4)) # or
rows.sort(lambda x, y : x[4] == y[4] and cmp(x[2],y[2]) or cmp(x[4], y[4]))
...not like I could just find the same info at the Python wiki or anything. :-P