David (dblume) wrote,
David
dblume

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
Tags: programming, python
Subscribe

  • Tron and the Realm of Fantastic Opportunity

    Around 1982, I was a teenager working off-the-books at the local arcade. I took out the trash, cleaned the front windows, the bathroom, and the…

  • Wonderful Early Father's Day

    We've got some neighborhood commitments on Father's Day, so my family decided to celebrate an early Father's Day today, just for me. My one…

  • Ichi

    I loved Ichi. I don't think this film got the attention it deserved. And strangely, I think it might be for the things it did right. I'll explain…

  • Post a new comment

    Error

    Comments allowed for friends only

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments