Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Sort by Extension by Kurush
from typing import List
import functools
def split_file_name(a):
n = a.rfind('.')
if n == -1: return (a, "")
if n == 0: return(a, "")
if n == len(a) - 1: return (a, "")
name = a[:n]
ext = a[n+1:]
return (name, ext)
def ext_cmp(a, b):
a_pair = split_file_name(a)
b_pair = split_file_name(b)
if (a_pair[1] > b_pair[1]) or (a_pair[1] == b_pair[1] and a_pair[0] > b_pair[0]):
return 1
elif (a_pair[1] == b_pair[1]) and (a_pair[0] == b_pair[0]):
return 0
else:
return -1
def sort_by_ext(files: List[str]) -> List[str]:
return sorted(files, key = functools.cmp_to_key(ext_cmp))
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!")
April 30, 2021
Comments: