-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort_by_name.sort.go
More file actions
64 lines (55 loc) · 1.38 KB
/
sort_by_name.sort.go
File metadata and controls
64 lines (55 loc) · 1.38 KB
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
package ossort
import (
"sort"
)
// Name 排序名称
type Name interface {
Name() string
}
// sortByName 排序
type sortByName struct{}
// NewSortByName .
func NewSortByName() SortByName {
return &sortByName{}
}
// Identifier 排序凭证
func (s *sortByName) Identifier(infos []Name) (newInfoSlice []*SortIdentifier) {
newInfoSlice = make([]*SortIdentifier, len(infos))
for i := range infos {
newInfoSlice[i] = &SortIdentifier{
OriginData: infos[i],
SortKey: GenSortName(infos[i].Name()),
}
}
return newInfoSlice
}
// Asc 文件名升序排序
func (s *sortByName) Asc(infos []Name) {
sortSlice := s.Identifier(infos)
sort.Slice(sortSlice, func(i, j int) bool {
return sortSlice[i].SortKey < sortSlice[j].SortKey
})
for i := range sortSlice {
infos[i] = sortSlice[i].OriginData.(Name)
}
return
}
// Desc 文件名倒序排序
func (s *sortByName) Desc(infos []Name) {
sortSlice := s.Identifier(infos)
sort.Slice(sortSlice, func(i, j int) bool {
return sortSlice[i].SortKey > sortSlice[j].SortKey
})
for i := range sortSlice {
infos[i] = sortSlice[i].OriginData.(Name)
}
return
}
// Slice 自定义排序 sort.Sort
func (s *sortByName) Slice(infos []Name, less func(i, j int) bool) {
sort.Slice(infos, less)
}
// SliceStable 自定义排序 sort.SliceStable
func (s *sortByName) SliceStable(infos []Name, less func(i, j int) bool) {
sort.SliceStable(infos, less)
}