0

I have two lists which need to be ordered. The first one work...

list = ['1.png', '2.png', '4.png', '5.png', '3.png', '6.png']
a = sorted(list)
print(a)

It sort the list by ascending order. In this second list, it sort it by the first lower digit..

list2 = ['10.png', '12.png', '8.png', '4.png', '22.png', '41.png']
a = sorted(list2)
print(a)

It print out this: ['10.png', '12.png', '22.png', '4.png', '41.png', '8.png']

How can I properly order it ?

lucians
  • 2,131
  • 3
  • 31
  • 61

1 Answers1

3

This list contain a list of str not integer

list2 = ['10.png', '12.png', '8.png', '4.png', '22.png', '41.png']
list2.sort(key = lambda x: int(x.split('.')[0]))

Output:

['4.png', '8.png', '10.png', '12.png', '22.png', '41.png']
khelili miliana
  • 3,537
  • 1
  • 14
  • 25