I am trying to sort a python list using sorted method as per the code below. However the sorting is not happening properly.
#sort using the number part of the string
mylist = ['XYZ-78.txt', 'XYZ-8.txt', 'XYZ-18.txt']
def func(elem):
return elem.split('-')[1].split('.')[0]
sortlist = sorted(mylist,key=func)
for i in sortlist:
print(i)
The output is-
XYZ-18.txt
XYZ-78.txt
XYZ-8.txt
I was expecting output as-
XYZ-8.txt
XYZ-18.txt
XYZ-78.txt