Skip to content

Commit 4bfbdd7

Browse files
committed
refactor(serverHandler/sort): adjust name sort struct
1 parent fbf63e1 commit 4bfbdd7

File tree

1 file changed

+41
-31
lines changed

1 file changed

+41
-31
lines changed

src/serverHandler/sort.go

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -33,77 +33,87 @@ var cmpDirMixed fnCompareDir = func(prev, next os.FileInfo) (less, ok bool) {
3333
return true, false
3434
}
3535

36-
// sort name asc
36+
// infosNames
3737

38-
type sortNameAscInfos struct {
38+
type infosNames struct {
3939
infos []os.FileInfo
4040
names [][]byte
4141
compareDir fnCompareDir
4242
}
4343

44-
func (sInfos sortNameAscInfos) Len() int {
45-
return len(sInfos.names)
44+
func (xInfos infosNames) Len() int {
45+
return len(xInfos.names)
4646
}
4747

48-
func (sInfos sortNameAscInfos) Less(i, j int) bool {
49-
less, ok := sInfos.compareDir(sInfos.infos[i], sInfos.infos[j])
50-
if ok {
51-
return less
52-
}
53-
54-
less, ok = util.CompareNumInFilename(sInfos.names[i], sInfos.names[j])
55-
if ok {
56-
return less
57-
}
48+
func (xInfos infosNames) Swap(i, j int) {
49+
xInfos.infos[i], xInfos.infos[j] = xInfos.infos[j], xInfos.infos[i]
50+
xInfos.names[i], xInfos.names[j] = xInfos.names[j], xInfos.names[i]
51+
}
5852

59-
return i < j
53+
func (xInfos infosNames) LessDir(i, j int) (less, ok bool) {
54+
return xInfos.compareDir(xInfos.infos[i], xInfos.infos[j])
6055
}
6156

62-
func (sInfos sortNameAscInfos) Swap(i, j int) {
63-
sInfos.infos[i], sInfos.infos[j] = sInfos.infos[j], sInfos.infos[i]
64-
sInfos.names[i], sInfos.names[j] = sInfos.names[j], sInfos.names[i]
57+
func (xInfos infosNames) LessFilename(i, j int) (less, ok bool) {
58+
return util.CompareNumInFilename(xInfos.names[i], xInfos.names[j])
6559
}
6660

67-
func newSortNameAscInfos(infos []os.FileInfo, compareDir fnCompareDir) sortNameAscInfos {
61+
func newInfosNames(infos []os.FileInfo, compareDir fnCompareDir) infosNames {
6862
names := make([][]byte, len(infos))
6963
for i := range infos {
7064
names[i] = []byte(infos[i].Name())
7165
}
7266

73-
return sortNameAscInfos{infos, names, compareDir}
67+
return infosNames{infos, names, compareDir}
68+
}
69+
70+
// sort name asc
71+
72+
type infosNamesAsc struct {
73+
infosNames
74+
}
75+
76+
func (xInfos infosNamesAsc) Less(i, j int) bool {
77+
less, ok := xInfos.LessDir(i, j)
78+
if ok {
79+
return less
80+
}
81+
82+
less, ok = xInfos.LessFilename(i, j)
83+
if ok {
84+
return less
85+
}
86+
87+
return i < j
7488
}
7589

7690
func sortInfoNamesAsc(infos []os.FileInfo, compareDir fnCompareDir) {
77-
nameCachedInfos := newSortNameAscInfos(infos, compareDir)
91+
nameCachedInfos := infosNamesAsc{newInfosNames(infos, compareDir)}
7892
sort.Sort(nameCachedInfos)
7993
}
8094

8195
// sort name desc
8296

83-
type sortNameDescInfos struct {
84-
sortNameAscInfos
97+
type infosNamesDesc struct {
98+
infosNames
8599
}
86100

87-
func (sInfos sortNameDescInfos) Less(i, j int) bool {
88-
less, ok := sInfos.compareDir(sInfos.infos[i], sInfos.infos[j])
101+
func (xInfos infosNamesDesc) Less(i, j int) bool {
102+
less, ok := xInfos.LessDir(i, j)
89103
if ok {
90104
return less
91105
}
92106

93-
less, ok = util.CompareNumInFilename(sInfos.names[j], sInfos.names[i])
107+
less, ok = xInfos.LessFilename(j, i)
94108
if ok {
95109
return less
96110
}
97111

98112
return j < i
99113
}
100114

101-
func newSortNameDescInfos(infos []os.FileInfo, compareDir fnCompareDir) sortNameDescInfos {
102-
return sortNameDescInfos{newSortNameAscInfos(infos, compareDir)}
103-
}
104-
105115
func sortInfoNamesDesc(infos []os.FileInfo, compareDir fnCompareDir) {
106-
nameCachedInfos := newSortNameDescInfos(infos, compareDir)
116+
nameCachedInfos := infosNamesDesc{newInfosNames(infos, compareDir)}
107117
sort.Sort(nameCachedInfos)
108118
}
109119

0 commit comments

Comments
 (0)