This can be done in many ways. But the code which I made I tried making it as elaborative and conception as possible I tried breaking each part in many subparts which can be done easily but to make understanding of the viewer tougher I broke the code into 3 parts the first part takes all the num value then second part sorts it in asc order then third part brings the whole file and makes a new list.i also put a feature that would accept duplicate values without any error I also added new items in image_list for improved understanding of viewer.
image_files = ["1.jpg", "3.jpg", "2.jpg", "4.jpg", "6.jpg", "5.jpg", "20.jpg", "29.jpg", "11.jpg", "9.jpg","5.img","6.img","100.png","20.jpg"]
name_of_folder_list = [] #list of ints in the fine name
for i in image_files:
num = i.split('.')[0]
name_of_folder_list.append(int(num))
asc_name_of_folder_list = sorted(name_of_folder_list, reverse=False)#sorting the number list in ascending order
output_list = []
for i in asc_name_of_folder_list:
for j in image_files:
if int(j.split('.')[0]) == int(i):
output_list.append(j)#this will add the item to new list
image_files.remove(j)#this will remove from old list avoiding double values of duplicates
else:
pass
print(output_list)
the output is:
['1.jpg', '2.jpg', '3.jpg', '4.jpg', '5.jpg', '5.img', '6.jpg', '6.img', '9.jpg', '11.jpg', '20.jpg', '20.jpg', '29.jpg', '100.png']
Process finished with exit code 0
This works with any format and is easily understood by beginners too.
Hope this helps you