So I have a list of strings that looks roughly like this:
list = ['file.t00Z.wrff02.grib2', 'file.t00Z.wrff03.grib2', 'file.t00Z.wrff00.grib2',
'file.t00Z.wrff05.grib2', 'file.t00Z.wrff04.grib2', 'file.t00Z.wrff01.grib2',
'file.t06Z.wrff01.grib2', 'file.t06Z.wrff00.grib2', 'file.t06Z.wrff02.grib2', ...]
I recently asked a question here wherein I learned how to sort my list of strings by substring using a lambda function:
list.sort(key=lambda x: x[x.find('wrff'):])
But now I need to know if there's a way to sort by two different substrings, almost like a composite primary key in a database. I'd like to sort the files first by the two digits following "file.t", and then by the two digits following "wrff". Is there a way that both of these actions can be performed at once?
SOLUTION: I wound up using the two-tuple lambda function sort that user Moses Koledoye recommended below, but I ran into problems when trying to apply this sorting process to groups of filenames with different naming conventions.
In my script I have 3 Python objects which grab files from unique data directories and form a list (like the one above) containing the files. Each of the objects grab files with different naming conventions, and each different group of files has a varying number of digit groups within their names.
To handle this without adding complexity, I decided to use the natsort module that user Jared Gougen suggested, and it worked very nicely.