def sorter(a, b):
# ensure ".." and all DirItems at start of order
if self.include_dirs:
- if isinstance(a, DirItem) and isinstance(b, DirItem):
- cmp_upper_dir = f' {UPPER_DIR}'
- if a.name == cmp_upper_dir:
- return +1
- if b.name == cmp_upper_dir:
- return -1
- elif isinstance(a, DirItem):
+ cmp_upper_dir = f' {UPPER_DIR}'
+ if isinstance(a, DirItem) and a.name == cmp_upper_dir:
+ return -1
+ if isinstance(b, DirItem) and b.name == cmp_upper_dir:
return +1
- elif isinstance(b, DirItem):
+ if isinstance(a, DirItem) and not isinstance(b, DirItem):
+ return -1
+ if isinstance(b, DirItem) and not isinstance(a, DirItem):
return +1
# apply self.sort_order within DirItems and FileItems (separately)
for key in self.sort_order:
- a_cmp = None
- b_cmp = None
- if hasattr(a, key):
- a_cmp = getattr(a, key)
- if hasattr(b, key):
- b_cmp = getattr(b, key)
- if a_cmp is None and b_cmp is not None:
+ if a_cmp is None and b_cmp is None:
+ continue
+ if a_cmp is None:
return -1
- elif a_cmp is not None and b_cmp is None:
+ if b_cmp is None:
return +1
- elif a_cmp > b_cmp:
+ if a_cmp > b_cmp:
return +1
- elif a_cmp < b_cmp:
+ if a_cmp < b_cmp:
return -1
return 0