I have a list of strings I would like to sort. However, some strings are one word, others are two words. Also, I would like to first sort by the second word, then the first.
I'm relatively new to the idea of lambda functions and also how a key works for the sort() method, so I've tried the following (which doesn't work):
list_to_sort = ['1 month', '1 year', '3 months', '3 years', '6 months', 'daily']
def sort_func(x):
x.split()
if len(x) == 1:
return x[0]
else:
return x[1]
cols.sort(key=sort_func)
# I would like the output to be:
# ['daily', '1 month', '3 months', '6 months', '1 year', '3 years']
Instead, the list doesn't change at all. I'm not too certain how the "key=" works in the sort() method though.