I have a list like this:
a = ['1', '3', '02', 'WF2', 'WF5', 'WF01']
and I want to sort like this:
a = ['1', '02', '3', 'WF01', 'WF2', 'WF5']
Using something like this:
def sortby(id):
if 'WF' not in id and id.isdigit():
return int(id)
elif 'WF' in id.upper():
return float('inf')
a.sort(key=sortby)
I can sort the integers without 'WF' prefix but I don't know how to sort the ones prefixed with 'WF' on its own.
Do I need to use double sorting, i.e. sort again and sort only the ones with prefix 'WF' and assign -Inf to all the other entries without 'WF' prefix? Any idea?
EDIT:
def sortby(id):
if 'WF' not in id.upper():
return int(id)
return float('inf')
def sortby2(id):
if 'WF' not in id.upper():
return float('-inf')
return int(id.replace('WF', ''))
a.sort(key=sortby)
a.sort(key=sortby2)
but it's not really nice...