0

I have a list of strings. They are actually files.

image_files = ["1.jpg", "3.jpg", "2.jpg", "4.jpg", "6.jpg", "5.jpg", "20.jpg", "29.jpg", "11.jpg", "9.jpg"]

I want to sort them like:

['1.jpg', '2.jpg', '3.jpg', '4.jpg', '5.jpg', '6.jpg', '9.jpg', '11.jpg', '20.jpg', '29.jpg']

I tried with sorted(image_files, key = len) but failed.

Fahad Ahammed
  • 371
  • 1
  • 11
  • 23
  • 2
    that isn't ascending, the list you already have is ascending. The problem is you want the strings to be *naturally ordered*, instead of lexicographically ordered (alphabetical). See this related: [question](https://stackoverflow.com/questions/4836710/is-there-a-built-in-function-for-string-natural-sort). In this case, you could just do something like `sorted(image_files, key=lambda x:int(x.partition('.')[0]))` – juanpa.arrivillaga May 29 '20 at 07:09

5 Answers5

2

try this,

sorted(image_files, key=lambda x : int(x.split(".")[0]))

['1.jpg', '2.jpg', '3.jpg', '4.jpg', ..]
sushanth
  • 8,275
  • 3
  • 17
  • 28
2

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

harsh jain
  • 395
  • 1
  • 3
  • 11
1

Sort by number:

import os

sorted_files = sorted(image_files, key=lambda x: int(os.path.splitext(x)[0]))

You could also zero-pad your file names to help with sorting.

Jan Christoph Terasa
  • 5,781
  • 24
  • 34
1
image_files = ["1.jpg", "3.jpg", "2.jpg", "4.jpg", "6.jpg", "5.jpg", "20.jpg", "29.jpg", "11.jpg", "9.jpg"]

image_files.sort(key=lambda item: (len(item), item))

print(image_files)

Try this.

0

Using list.sort() method to solve this

image_files = ["1.jpg", "3.jpg", "2.jpg", "4.jpg", "6.jpg", "5.jpg", "20.jpg", "29.jpg", "11.jpg", "9.jpg"]
image_files.sort(key=lambda x : int(x.split('.')[0]))
print(image_files)
#['1.jpg', '2.jpg', '3.jpg', '4.jpg', '5.jpg', '6.jpg', '9.jpg', '11.jpg', '20.jpg', '29.jpg']
Krishna Singhal
  • 631
  • 6
  • 9