-
Notifications
You must be signed in to change notification settings - Fork 2
/
sort
96 lines (87 loc) · 1.75 KB
/
sort
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
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
)
var (
dir string
split string
index int
suffix string
fullpath bool
)
func init() {
flag.StringVar(&dir, "d", "./", `-d ./ 指定排序的文件目录`)
flag.StringVar(&split, "S", ".", `-S "." 指定分隔符`)
flag.IntVar(&index, "i", 0, "-i 0 指定使用哪一列进行排序")
flag.StringVar(&suffix, "s", "", `-s ".sql" 指定排序文件的后缀`)
flag.BoolVar(&fullpath, "p", false, "-p true 是否打印完整路径")
flag.Parse()
}
func main() {
path, err := os.Open(dir)
if err != nil {
fmt.Println(err)
return
}
defer path.Close()
info, err := path.Stat()
if err != nil {
fmt.Println(err)
return
}
if !info.IsDir() {
fmt.Println("指定的路径必须是文件目录")
return
}
files, err := path.Readdir(-1)
if err != nil {
fmt.Println(err)
return
}
var list = make(byName, 0, len(files))
for _, name := range files {
if !strings.HasSuffix(name.Name(), suffix) {
continue
}
list = append(list, name.Name())
}
sort.Sort(list)
basedir := filepath.Dir(dir)
for _, path := range list {
if fullpath {
fmt.Println(basedir + string(filepath.Separator) + path)
} else {
fmt.Println(path)
}
}
}
type byName []string
func (f byName) Len() int { return len(f) }
func (f byName) Less(i, j int) bool {
list := strings.Split(f[i], split)
if index > len(list)-1 {
return false
}
a := list[index]
list = strings.Split(f[j], split)
if index > len(list)-1 {
return false
}
b := list[index]
aint, err := strconv.Atoi(a)
if err != nil {
return a < b
}
bint, err := strconv.Atoi(b)
if err != nil {
return a < b
}
return aint < bint
}
func (f byName) Swap(i, j int) { f[i], f[j] = f[j], f[i] }