Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Sort by Extension by dumatkachuk
def sort_by_ext(lst):
'''Function sorts the list of files by extension.
The files with the same extension sorted by name.'''
# sorted list of files without extension
without_ext = sorted([i for i in lst if i.rfind('.') == 0])
# list of files with extension
with_ext = [i for i in lst if i.rfind('.') != 0]
# with_ext sorted by: first by extension, second by name
return without_ext + sorted(with_ext, key=lambda x: (x[x.rfind('.')+1:], x[:x.rfind('.')]))
if __name__ == '__main__':
print("Example:")
print(sort_by_ext(['1.cad', '1.bat', '1.aa']))
# These "asserts" are used for self-checking and not for an auto-testing
assert sort_by_ext(['1.cad', '1.bat', '1.aa']) == ['1.aa', '1.bat', '1.cad']
assert sort_by_ext(['1.cad', '1.bat', '1.aa', '2.bat']) == ['1.aa', '1.bat', '2.bat', '1.cad']
assert sort_by_ext(['1.cad', '1.bat', '1.aa', '.bat']) == ['.bat', '1.aa', '1.bat', '1.cad']
assert sort_by_ext(['1.cad', '1.bat', '.aa', '.bat']) == ['.aa', '.bat', '1.bat', '1.cad']
assert sort_by_ext(['1.cad', '1.', '1.aa']) == ['1.', '1.aa', '1.cad']
assert sort_by_ext(['1.cad', '1.bat', '1.aa', '1.aa.doc']) == ['1.aa', '1.bat', '1.cad', '1.aa.doc']
assert sort_by_ext(['1.cad', '1.bat', '1.aa', '.aa.doc']) == ['1.aa', '1.bat', '1.cad', '.aa.doc']
print("Coding complete? Click 'Check' to earn cool rewards!")
Jan. 3, 2021