tags:

views:

92

answers:

3
+1  A: 

There are two ways to approach this:

  1. 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.

  2. 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:

m = re.match('Season ([0-9]+), Episode ([0-9]+): .*', s)
(season, episode) = (int(m.group(1)), int(m.group(2)))
dmazzoni
Thanks. I got halfway through building a function then got frustrated and used the natsorted method that was linked. A solid foundation though!
matt
`cmp` is gone in Python3, so it's a good idea (and more efficient) to get used to using `key` instead
gnibbler
A: 

Since you're sorting by strings, "1" comes before "10", so your intended episodes will not be in proper order. The solution is to pull apart the string into its constituent parts, namely get the season and episodes as integers, place them in an associative data structure then sort by the relevant integers. For pulling apart the string into its parts, check out Python's Regular Expressions, cast the season number and episode numbers as integers, then pick a data structure you like and associate the integer keys with the strings. Sort by the keys, and you're done.

reshen
+3  A: 

Use the key parameter to the sort function to specify the key you would like to use for sorting.

def get_sort_key(s):
    m = re.match('Season ([0-9]+), Episode ([0-9]+): .*', s)
    return (int(m.group(1)), int(m.group(2)))

my_list.sort(key=get_sort_key)
Daniel Stutzbach