I have a dictionary named 'times' which maps keys to string values that represent a time:
times = {'key1': '12.23', 'key2': '43:53.29', 'key3': '1:38:11.50r'}
The string takes the form of [hours]:[minutes]:[seconds].[milliseconds][r] where every field is optional. The r is a flag that doesn't depend on any other values being filled in and doesn't factor into sorting. [hours] requires that [minutes] and down are present, but [minutes] doesn't require [hours] to be present.
I want to end up with a list of keys sorted by the time-ordering of their values.
I have the following:
standings = sorted(times, key=times.__getitem__)
but it only sorts based on a string value. I'm new to python, but if I were using java I would probably write a Time class with a custom compareTo() function to get the sort to work.
I could write a function that converts the string to a time in milliseconds, then sort based on that, but don't know how I would do so using 'key=' in the sorted() function.