-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
  • 4,955
  • 5
  • 42
  • 92
  • 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
  • 4,955
  • 5
  • 42
  • 92
1
my_list.sort(key = lambda x : x[-6:])
gsb22
  • 1,930
  • 2
  • 7
  • 22