aboutsummaryrefslogtreecommitdiff
path: root/internal/gallery/query_sort.go
blob: 1a3a63cda32b4dc5942e3bf6c51e41a5b2ac11be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
		}
	})
}