home · contact · privacy
In browser.py, fix continuously broken sorting algorithm.
authorChristian Heller <c.heller@plomlompom.de>
Mon, 9 Sep 2024 23:58:29 +0000 (01:58 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Mon, 9 Sep 2024 23:58:29 +0000 (01:58 +0200)
browser.py

index 41b33d547dfbc75fba062ef29a24be01a8cba56c..9de8ca7cb2b56ccf51b31cfbcc43fbc41e100d7c 100755 (executable)
@@ -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