-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
68 lines (58 loc) · 1.17 KB
/
file.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
package main
import (
"io/ioutil"
"sort"
"strconv"
)
func dirs(dir string) []string {
files, err := ioutil.ReadDir(dir)
if err != nil {
panic(err)
}
l := len(files)
names := make([]string, 0, l)
for _, f := range files {
if f.IsDir() {
names = append(names, f.Name())
}
}
sort.Strings(names)
return names
}
type numericint struct {
strs []string
}
// Len is part of sort.Interface.
func (n *numericint) Len() int {
return len(n.strs)
}
// Swap is part of sort.Interface.
func (n *numericint) Swap(i, j int) {
n.strs[i], n.strs[j] = n.strs[j], n.strs[i]
}
// Less is part of sort.Interface. It is implemented by calling the "by" closure in the sorter.
func (n *numericint) Less(i, j int) bool {
iint, err := strconv.Atoi(n.strs[i][:len(n.strs[i])-5])
if err != nil {
panic(err)
}
jint, err := strconv.Atoi(n.strs[j][:len(n.strs[j])-5])
if err != nil {
panic(err)
}
return iint < jint
}
func files(dir string) []string {
files, err := ioutil.ReadDir(dir)
if err != nil {
panic(err)
}
l := len(files)
names := make([]string, l, l)
for i, f := range files {
names[i] = f.Name()
}
n := &numericint{strs: names}
sort.Sort(n)
return n.strs
}