-1

Say my list looks like this

['abda01.txt', 'fdafe10.txt', 'ytn05.txt', 'a02.txt' ]

And I want to sort by the last 6 characters only, so the result looks like this

['abda01.txt', 'a02.txt', 'ytn05.txt', 'fdafe10.txt']

SantoshGupta7
  • 5,607
  • 14
  • 58
  • 116
  • 1
    Does this answer your question? [Sort strings by the first N characters](https://stackoverflow.com/questions/2289870/sort-strings-by-the-first-n-characters). Adopting this to the last N characters should be trivial if you know [slicing](https://stackoverflow.com/questions/509211/understanding-slice-notation). – Georgy Feb 12 '20 at 13:02

2 Answers2

1

Use

sorted(files, key=lambda x: (x[-7:]))
SantoshGupta7
  • 5,607
  • 14
  • 58
  • 116
1
my_list.sort(key = lambda x : x[-6:])
gsb22
  • 2,112
  • 2
  • 10
  • 25