0

I have a list like this

a = ['data/1.jpg','data/10.jpg','data/2.jpg'...]

I want to sort this list like this manner,

['data/1.jpg','data/2.jpg','data/10.jpg'...]

I have tried lots of methods, But it is not working, How I will do?

mx0
  • 6,445
  • 12
  • 49
  • 54
Sudip Das
  • 1,178
  • 1
  • 9
  • 24

1 Answers1

1

You need to write a function that parses the file names the way you want, and use that as your sort key. For the example you've given, you could do this:

def key(full_name):
    name, _ = full_name.split(".")
    name, order = name.split("/")
    return name, int(order)

foo = ['data/1.jpg','data/10.jpg','data/2.jpg']
bar = sorted(foo, key=key)
Batman
  • 8,571
  • 7
  • 41
  • 80