short of renaming/fixing the logging module on the webservers… when i do a list.sort(), the list entries get placed in the following order:
2011-09-21 19:15:54,731 DEBUG __main__ 44: running www.site.com-110731.log.0.gz
2011-09-21 19:15:54,731 DEBUG __main__ 44: running www.site.com-110731.log.1.gz
2011-09-21 19:15:54,731 DEBUG __main__ 44: running www.site.com-110731.log.2.gz
2011-09-21 19:15:54,732 DEBUG __main__ 44: running www.site.com-110731.log.3.gz
2011-09-21 19:15:54,732 DEBUG __main__ 44: running www.site.com-110731.log.gz
how would i sort a list, to get (ie the entry eithout a digit to be first):
2011-09-21 19:15:54,732 DEBUG __main__ 44: running www.site.com-110731.log.gz
2011-09-21 19:15:54,731 DEBUG __main__ 44: running www.site.com-110731.log.0.gz
2011-09-21 19:15:54,731 DEBUG __main__ 44: running www.site.com-110731.log.1.gz
2011-09-21 19:15:54,731 DEBUG __main__ 44: running www.site.com-110731.log.2.gz
2011-09-21 19:15:54,732 DEBUG __main__ 44: running www.site.com-110731.log.3.gz
THANKS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
You would probably want to write a custom comparator to pass to
sort; in fact, you probably need to anyway, because you’re likely getting a lexicographical sort order instead of the intended (I presume) numerical order.For instance, if you know that the filenames will only differ in those digits, you’d write a comparator that extracts those digits, converts them to
int, and then compares based on that value.Taking your examples as canonical, your comparator might look something like this:
This prefers to sort based on the “file” number (the first one), and then by the “index” number (the second one). Note that it takes advantage of the fact that using
cmpon tuples works as we require.