diff options
| author | RanXom <saiyedshizain@gmail.com> | 2026-07-18 22:40:52 +0530 |
|---|---|---|
| committer | RanXom <saiyedshizain@gmail.com> | 2026-07-18 22:40:52 +0530 |
| commit | eb6a8c0306c44d5317c8d321ae24537bac9434ed (patch) | |
| tree | 4fa8310e498441e93efd1b23584bee55cef2af78 /internal/gallery/query_sort.go | |
| parent | 8983d196786b86045920c0ee0e03711fbf62dfe8 (diff) | |
| download | galleryd-eb6a8c0306c44d5317c8d321ae24537bac9434ed.tar.xz galleryd-eb6a8c0306c44d5317c8d321ae24537bac9434ed.zip | |
feat(gallery): support sorted photo queries
Diffstat (limited to 'internal/gallery/query_sort.go')
| -rw-r--r-- | internal/gallery/query_sort.go | 40 |
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..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 + } + }) +} |