diff options
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 + } + }) +} |