-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorder.go
75 lines (60 loc) · 1.52 KB
/
order.go
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
package main
import (
"log"
"io/fs"
)
// ByName implements sort.Interface for []DirEntry based on the Name() field.
type ByName []fs.DirEntry
// sort.Interface requires Len, Swap and Less
func (a ByName) Len() int {
return len(a)
}
func (a ByName) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}
func (a ByName) Less(i, j int) bool {
return a[i].Name() < a[j].Name()
}
// ByMod implements sort.Interface for []DirEntry based on the Name() field.
type ByMod []fs.DirEntry
// sort.Interface requires Len, Swap and Less
func (a ByMod) Len() int {
return len(a)
}
func (a ByMod) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}
func (a ByMod) Less(i, j int) bool {
iinfo, err := a[i].Info()
if err != nil {
log.Printf("Error getting ModTime of %s: %s", a[i].Name(), err)
return true
}
jinfo, err := a[j].Info()
if err != nil {
log.Printf("Error getting ModTime of %s: %s", a[j].Name(), err)
return false
}
// Last Modified First
return iinfo.ModTime().After(jinfo.ModTime())
}
// BySize implements sort.Interface for []DirEntry based on the Name() field.
type BySize []fs.DirEntry
// sort.Interface requires Len, Swap and Less
func (a BySize) Len() int {
return len(a)
}
func (a BySize) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}
func (a BySize) Less(i, j int) bool {
iinfo, err := a[i].Info()
if err != nil {
log.Fatalf("Error getting ModTime of %s: %s", a[i].Name(), err)
}
jinfo, err := a[j].Info()
if err != nil {
log.Fatalf("Error getting ModTime of %s: %s", a[j].Name(), err)
}
return iinfo.Size() < jinfo.Size()
}