I’ve got the following python code that does a date comparison for a list sort function (e.g. date_list.sort(cmp=dcmp). I’m wondering if it’s possibly to remove the cmp/if testing and make it a oneliner.
def dcmp(a, b):
amm, add, ayy = [int(v) for v in a.split('/')]
bmm, bdd, byy = [int(v) for v in b.split('/')]
v = cmp(ayy, byy)
if v != 0: return v
v = cmp(amm, bmm)
if v != 0: return v
return cmp(add, bdd)
Update: Let me make it clear what I’m curious about is can you remove this block of code from the function:
v = cmp(ayy, byy)
if v != 0: return v
v = cmp(amm, bmm)
if v != 0: return v
return cmp(add, bdd)
Figured it out, what I was looking for was to replace just this code (ignore that it was dates)
The correct answer is:
Which can also be written as: