From 5e8035cb1d1a385f2221c78678de244cf9a9cfd7 Mon Sep 17 00:00:00 2001 From: pax Date: Sat, 11 Apr 2026 21:59:20 -0500 Subject: [PATCH] library: fix Post ID sort for templated filenames Post ID sort used filepath.stem which sorted templated filenames like artist_12345.jpg alphabetically instead of by post ID. Now resolves post_id via library_meta DB lookup, falls back to digit-stem for legacy files, unknowns sort to the end. --- booru_viewer/gui/library.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/booru_viewer/gui/library.py b/booru_viewer/gui/library.py index a8b06f1..e03ec9d 100644 --- a/booru_viewer/gui/library.py +++ b/booru_viewer/gui/library.py @@ -274,14 +274,18 @@ class LibraryView(QWidget): def _sort_files(self) -> None: mode = self._sort_combo.currentText() if mode == "Post ID": - # Numeric sort by post id (filename stem). Library files are - # named {post_id}.{ext} in normal usage; anything with a - # non-digit stem (someone manually dropped a file in) sorts - # to the end alphabetically so the numeric ordering of real - # posts isn't disrupted by stray names. + # Numeric sort by post id. Resolves templated filenames + # (e.g. artist_12345.jpg) via library_meta DB lookup, falls + # back to digit-stem parsing for legacy files. Anything + # without a resolvable post_id sorts to the end alphabetically. def _key(p: Path) -> tuple: - stem = p.stem - return (0, int(stem)) if stem.isdigit() else (1, stem.lower()) + if self._db: + pid = self._db.get_library_post_id_by_filename(p.name) + if pid is not None: + return (0, pid) + if p.stem.isdigit(): + return (0, int(p.stem)) + return (1, p.stem.lower()) self._files.sort(key=_key) elif mode == "Size": self._files.sort(key=lambda p: p.stat().st_size, reverse=True)