先根据后缀名 然后根据文件名,排序文件名列表
def cmp_fun(x, y):
if x.split('.')[-1] > y.split('.')[-1]:
return 1
elif x.split('.')[-1] < y.split('.')[-1]:
return -1
else :
if x.split('.')[0] > y.split('.')[0]:
return 1
elif x.split('.')[0] < y.split('.')[0]:
return -1
else:
return 0
def extsort(filenames):
"""Sorts given list of filenames based on extension.
>>> extsort(['a.c', 'a.py', 'b.py', 'bar.txt', 'foo.txt', 'x.c'])
['a.c', 'x.c', 'a.py', 'b.py', 'bar.txt', 'foo.txt']
"""
# your code here
return sorted(filenames, cmp=cmp_fun)