Python has built in sort functions.  But I don't think you're really sorting, it appeared as though you are filtering.  Which could be done, for example, using a list comprehension.