-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsorting.go
More file actions
100 lines (77 loc) · 2.47 KB
/
sorting.go
File metadata and controls
100 lines (77 loc) · 2.47 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package mfGalleryMetaCreatorGo
import (
"log"
"sort"
)
// available image order functions
var IMAGE_ORDER_FUNCTIONS = [...]string{"exifTimeAsc", "exifTimeDesc", "filenameAsc", "filenameDesc"}
func SortImages(orderFunctionName string, images JsonImages) {
switch orderFunctionName {
case IMAGE_ORDER_FUNCTIONS[0]:
sort.Sort(byExifTimeAsc{images})
break
case IMAGE_ORDER_FUNCTIONS[1]:
sort.Sort(byExifTimeDesc{images})
break
case IMAGE_ORDER_FUNCTIONS[2]:
sort.Sort(byFilenameAsc{images})
break
case IMAGE_ORDER_FUNCTIONS[3]:
sort.Sort(byFilenameDesc{images})
break
default:
log.Fatal("Not supported:", orderFunctionName)
}
}
// sort functions for images
type JsonImages []MetaJsonImage
func (s JsonImages) Len() int { return len(s) }
func (s JsonImages) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
type byExifTimeAsc struct{ JsonImages }
func (s byExifTimeAsc) Less(i, j int) bool {
return lessTimeNameAsc(s.JsonImages[i].Exif.Time, s.JsonImages[j].Exif.Time,
s.JsonImages[i].Filename, s.JsonImages[j].Filename)
}
type byExifTimeDesc struct{ JsonImages }
func (s byExifTimeDesc) Less(i, j int) bool {
return lessTimeNameDesc(s.JsonImages[i].Exif.Time, s.JsonImages[j].Exif.Time,
s.JsonImages[i].Filename, s.JsonImages[j].Filename)
}
type byFilenameAsc struct{ JsonImages }
func (s byFilenameAsc) Less(i, j int) bool {
return s.JsonImages[i].Filename < s.JsonImages[j].Filename
}
type byFilenameDesc struct{ JsonImages }
func (s byFilenameDesc) Less(i, j int) bool {
return s.JsonImages[i].Filename >= s.JsonImages[j].Filename
}
// sort functions for directories
type JsonDirs []MetaJsonSubDir
func (s JsonDirs) Len() int { return len(s) }
func (s JsonDirs) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
type ByTimeDesc struct{ JsonDirs }
func (s ByTimeDesc) Less(i, j int) bool {
return lessTimeNameDesc(s.JsonDirs[i].Time, s.JsonDirs[j].Time,
s.JsonDirs[i].Title, s.JsonDirs[j].Title)
}
// generic sort functsion
// lowest date first, 'without time' is always the last
func lessTimeNameAsc(timeA, timeB *int64, nameA, nameB string) bool {
if timeA != nil && timeB != nil {
return *timeA < *timeB
}
if timeA == nil && timeB == nil {
return nameA < nameB
}
return timeA != nil
}
// highest date first, 'without time' is always the last
func lessTimeNameDesc(timeA, timeB *int64, nameA, nameB string) bool {
if timeA != nil && timeB != nil {
return *timeA >= *timeB
}
if timeA == nil && timeB == nil {
return nameA < nameB
}
return timeA != nil
}