I have a list like
mylist=['foo/2.py','foo/12.py','foo/10.py','foo/1.py','foo/25.py']
and i need to get the index of the sort like
[3,2,1,0,4]
The answers of How to get indices of a sorted array in Python question's are not working as it get the index of a non natural sort
def natural_keys(text):
return [ atoi(c) for c in re.split(r'(\d+)', text) ]
mylist=['foo/2.py','foo/12.py','foo/10.py','foo/1.py','foo/25.py']
print(mylist)
mylist.sort(key=natural_keys)
print(mylist)
ind=[i[0] for i in sorted(enumerate(mylist), key=lambda x:x[1])]
print(ind)
mylist=[mylist[i] for i in ind]
print(mylist)
gives
['foo/2.py', 'foo/12.py', 'foo/10.py', 'foo/1.py', 'foo/25.py']
['foo/1.py', 'foo/2.py', 'foo/10.py', 'foo/12.py', 'foo/25.py']
[0, 2, 3, 1, 4]
['foo/1.py', 'foo/10.py', 'foo/12.py', 'foo/2.py', 'foo/25.py']