From 38032cacf5d1de769acbdbd277fc01b554e3cc10 Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Tue, 10 Sep 2024 01:58:29 +0200
Subject: [PATCH] In browser.py, fix continuously broken sorting algorithm.

---
 browser.py | 31 +++++++++++++------------------
 1 file changed, 13 insertions(+), 18 deletions(-)

diff --git a/browser.py b/browser.py
index 41b33d5..9de8ca7 100755
--- a/browser.py
+++ b/browser.py
@@ -277,31 +277,26 @@ class MainWindow(Gtk.Window):
         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
 
-- 
2.30.2