Possible Duplicate:
Python analog of natsort function (sort a list using a “natural order” algorithm)
I’m sure this is simple but I can’t figure it out. I have a list of strings like this(after using sorted on it):
Season 2, Episode 1: A Flight to Remember
Season 2, Episode 20: Anthology of Interest I
Season 2, Episode 2: Mars University
Season 2, Episode 3: When Aliens Attack
....
Season 3, Episode 10: The Luck of the Fryrish
Season 3, Episode 11: The Cyber House Rules
Season 3, Episode 12: Insane in the Mainframe
Season 3, Episode 1: The Honking
Season 3, Episode 2: War Is the H-Word
How can I make them sort out properly? (by season then episode #, ascending)
There are two ways to approach this:
Define your own sorting function cmp(x, y), where x and y are strings, and you return 1 if the second one is greater than the first, -1 if the first is greater, and 0 if they’re the same. Then pass this function as the “cmp” argument to the built-in sort() function.
Convert all of the strings into a format where the “natural” sorting order is exactly what you want. For example you could just zero-pad them like “Season 03, Episode 07”. Then you can sort them using sort().
Either way, I’d suggest using a simple regular expression to get the season and episode out of the string, something like: