-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathsort.go
67 lines (57 loc) · 1.26 KB
/
sort.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
package main
// BySize tells `sort.Sort` how to sort by file size
type BySize []*DisplayItem
func (s BySize) Less(i, j int) bool {
return s[i].info.Size() < s[j].info.Size()
}
func (s BySize) Len() int {
return len(s)
}
func (s BySize) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
// ByTime tells `sort.Sort` how to sort by last modified time
type ByTime []*DisplayItem
func (s ByTime) Less(i, j int) bool {
return s[i].info.ModTime().Unix() < s[j].info.ModTime().Unix()
}
func (s ByTime) Len() int {
return len(s)
}
func (s ByTime) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
// ByKind tells `sort.Sort` how to sort by file extension
type ByKind []*DisplayItem
func (s ByKind) Less(i, j int) bool {
var kindi, kindj string
if s[i].IsHidden() {
kindi = "." + s[i].ext
} else if s[i].ext == "" {
kindi = "0"
} else {
kindi = s[i].ext
}
if s[j].IsHidden() {
kindj = "." + s[j].ext
} else if s[j].ext == "" {
kindj = "0"
} else {
kindj = s[j].ext
}
if kindi == kindj {
return s[i].basename < s[j].basename
}
return kindi < kindj
}
func (s ByKind) Len() int {
return len(s)
}
func (s ByKind) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func reverse(s []*DisplayItem) {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
}