@@ -2,6 +2,7 @@ package serverHandler
22
33import (
44 "../util"
5+ "bytes"
56 "os"
67 "sort"
78 "strings"
@@ -58,6 +59,35 @@ func (xInfos infosNames) LessFilename(i, j int) (less, ok bool) {
5859 return util .CompareNumInFilename (xInfos .names [i ], xInfos .names [j ])
5960}
6061
62+ func (xInfos infosNames ) LessFileType (i , j int ) (less , ok bool ) {
63+ bufferI := xInfos .names [i ]
64+ bufferJ := xInfos .names [j ]
65+ for {
66+ dotIndexI := bytes .LastIndexByte (bufferI , '.' )
67+ dotIndexJ := bytes .LastIndexByte (bufferJ , '.' )
68+ if dotIndexI < 0 && dotIndexJ < 0 {
69+ break
70+ }
71+ if dotIndexI < 0 {
72+ return true , true
73+ }
74+ if dotIndexJ < 0 {
75+ return false , true
76+ }
77+
78+ typeI := bufferI [dotIndexI + 1 :]
79+ typeJ := bufferJ [dotIndexJ + 1 :]
80+ less , ok = util .CompareNumInFilename (typeI , typeJ )
81+ if ok {
82+ return less , ok
83+ }
84+ bufferI = bufferI [:dotIndexI ]
85+ bufferJ = bufferJ [:dotIndexJ ]
86+ }
87+
88+ return util .CompareNumInFilename (bufferI , bufferJ )
89+ }
90+
6191func newInfosNames (infos []os.FileInfo , compareDir fnCompareDir ) infosNames {
6292 names := make ([][]byte , len (infos ))
6393 for i := range infos {
@@ -117,6 +147,56 @@ func sortInfoNamesDesc(infos []os.FileInfo, compareDir fnCompareDir) {
117147 sort .Sort (nameCachedInfos )
118148}
119149
150+ // sort type asc
151+
152+ type infosTypesAsc struct {
153+ infosNames
154+ }
155+
156+ func (xInfos infosTypesAsc ) Less (i , j int ) bool {
157+ less , ok := xInfos .LessDir (i , j )
158+ if ok {
159+ return less
160+ }
161+
162+ less , ok = xInfos .LessFileType (i , j )
163+ if ok {
164+ return less
165+ }
166+
167+ return i < j
168+ }
169+
170+ func sortInfoTypesAsc (infos []os.FileInfo , compareDir fnCompareDir ) {
171+ nameCachedInfos := infosTypesAsc {newInfosNames (infos , compareDir )}
172+ sort .Sort (nameCachedInfos )
173+ }
174+
175+ // sort type desc
176+
177+ type infosTypesDesc struct {
178+ infosNames
179+ }
180+
181+ func (xInfos infosTypesDesc ) Less (i , j int ) bool {
182+ less , ok := xInfos .LessDir (i , j )
183+ if ok {
184+ return less
185+ }
186+
187+ less , ok = xInfos .LessFileType (j , i )
188+ if ok {
189+ return less
190+ }
191+
192+ return j < i
193+ }
194+
195+ func sortInfoTypesDesc (infos []os.FileInfo , compareDir fnCompareDir ) {
196+ nameCachedInfos := infosTypesDesc {newInfosNames (infos , compareDir )}
197+ sort .Sort (nameCachedInfos )
198+ }
199+
120200// sort size asc
121201
122202func sortInfoSizesAsc (infos []os.FileInfo , compareDir fnCompareDir ) {
@@ -270,6 +350,10 @@ func sortInfos(infos []os.FileInfo, rawQuery string, defaultSortBy string) (rawS
270350 sortInfoNamesAsc (infos , compareDir )
271351 case 'N' :
272352 sortInfoNamesDesc (infos , compareDir )
353+ case 'e' :
354+ sortInfoTypesAsc (infos , compareDir )
355+ case 'E' :
356+ sortInfoTypesDesc (infos , compareDir )
273357 case 's' :
274358 sortInfoSizesAsc (infos , compareDir )
275359 case 'S' :
0 commit comments