aboutsummaryrefslogtreecommitdiff
path: root/internal/gallery/query_sort.go
diff options
context:
space:
mode:
authorSaiyed Shizain <saiyedshizain@gmail.com>2026-07-22 17:32:55 +0530
committerGitHub <noreply@github.com>2026-07-22 17:32:55 +0530
commit9b541ca2d0a639eebd9610fcd6385e841da68e0a (patch)
treed331e8d858f62cf82d0cb6c3ca96400d4e28edca /internal/gallery/query_sort.go
parent46d2bf8bb5a0d18bdbe613a9346a5757b39e0a35 (diff)
parentddd8a37a0eb5623960e4528802263c7cec23af87 (diff)
downloadgalleryd-9b541ca2d0a639eebd9610fcd6385e841da68e0a.tar.xz
galleryd-9b541ca2d0a639eebd9610fcd6385e841da68e0a.zip
Merge pull request #13 from RanXom/feat/api
Feat/api: add sorting support to gallery queries
Diffstat (limited to 'internal/gallery/query_sort.go')
-rw-r--r--internal/gallery/query_sort.go40
1 files changed, 40 insertions, 0 deletions
diff --git a/internal/gallery/query_sort.go b/internal/gallery/query_sort.go
new file mode 100644
index 0000000..1a3a63c
--- /dev/null
+++ b/internal/gallery/query_sort.go
@@ -0,0 +1,40 @@
+package gallery
+
+import "sort"
+
+func sortQueryPhotos(
+ photos []Photo,
+ query Query,
+) {
+ if query.Sort == "" {
+ return
+ }
+
+ sort.SliceStable(photos, func(i, j int) bool {
+ switch query.Sort {
+ case SortByPath:
+ if query.Order == SortDesc {
+ return photos[i].RelativePath > photos[j].RelativePath
+ }
+
+ return photos[i].RelativePath < photos[j].RelativePath
+ case SortByDateTaken:
+ if query.Order == SortAsc {
+ if !photos[i].DateTaken.Equal(photos[j].DateTaken) {
+ return photos[i].DateTaken.Before(photos[j].DateTaken)
+ }
+
+ return photos[i].RelativePath < photos[j].RelativePath
+ }
+
+ if !photos[i].DateTaken.Equal(photos[j].DateTaken) {
+ return photos[i].DateTaken.After(photos[j].DateTaken)
+ }
+
+ return photos[i].RelativePath < photos[j].RelativePath
+
+ default:
+ return false
+ }
+ })
+}