From eb6a8c0306c44d5317c8d321ae24537bac9434ed Mon Sep 17 00:00:00 2001 From: RanXom Date: Sat, 18 Jul 2026 22:40:52 +0530 Subject: feat(gallery): support sorted photo queries --- internal/gallery/query_sort.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 internal/gallery/query_sort.go (limited to 'internal/gallery/query_sort.go') diff --git a/internal/gallery/query_sort.go b/internal/gallery/query_sort.go new file mode 100644 index 0000000..7ad811b --- /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 SortByDateTime: + 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 + } + }) +} -- cgit v1.2.3