Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
oop magic solution in Clear category for Sort by Extension by ssk8
from typing import List
class File:
def __init__(self, full):
self.full = full
self.parts = [p for p in full.split('.') if p]
@property
def ext(self):
has_ext = len(self.parts) > 1 and not self.full.endswith('.')
return self.parts[-1] if has_ext else False
def __lt__(self, o):
if self.ext and o.ext:
return self.full < o.full if self.ext == o.ext else self.ext < o.ext
else:
return bool(o.ext)
def sort_by_ext(files: List[str]) -> List[str]:
files = [File(file) for file in files]
return [file.full for file in sorted(files)]
Feb. 24, 2021