/
sort.go
109 lines (91 loc) 路 2.4 KB
/
sort.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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
package webindexer
import (
"sort"
"strconv"
"unicode"
)
func (i *Indexer) sort(items *[]Item) {
switch i.Cfg.SortByValue() {
case SortByDate:
orderByLastModified(items)
case SortByName:
orderByName(items)
case SortByNaturalName:
orderByNaturalName(items)
}
if i.Cfg.OrderByValue() == OrderDesc {
sort.SliceStable(*items, func(i, j int) bool {
return !cmpNatural((*items)[i].Name, (*items)[j].Name)
})
}
if i.Cfg.DirsFirst {
orderDirsFirst(items)
}
}
func orderByName(items *[]Item) {
sort.SliceStable(*items, func(i, j int) bool {
return (*items)[i].Name < (*items)[j].Name
})
}
func orderByLastModified(items *[]Item) {
sort.SliceStable(*items, func(i, j int) bool {
return (*items)[i].LastModified > (*items)[j].LastModified
})
}
// orderByNaturalName sorts items by their names with numbers ordered
// naturally. e.g. 1,2,10 instead of 1,10,2 or 0.8.2 before 0.8.10
func orderByNaturalName(items *[]Item) {
sort.SliceStable(*items, func(i, j int) bool {
return cmpNatural((*items)[i].Name, (*items)[j].Name)
})
}
// parseSegments splits a string into numeric and non-numeric segments.
func parseSegments(s string) []string {
var segments []string
var currentSegment string
for _, char := range s {
if len(currentSegment) == 0 || unicode.IsDigit(rune(currentSegment[0])) == unicode.IsDigit(char) {
currentSegment += string(char)
} else {
segments = append(segments, currentSegment)
currentSegment = string(char)
}
}
if currentSegment != "" {
segments = append(segments, currentSegment)
}
return segments
}
// cmpNatural compares two strings naturally.
func cmpNatural(a, b string) bool {
aSegments := parseSegments(a)
bSegments := parseSegments(b)
for i := 0; i < len(aSegments) && i < len(bSegments); i++ {
if aSegments[i] == bSegments[i] {
continue
}
aIsDigit := unicode.IsDigit(rune(aSegments[i][0]))
bIsDigit := unicode.IsDigit(rune(bSegments[i][0]))
if aIsDigit && bIsDigit {
an, _ := strconv.Atoi(aSegments[i])
bn, _ := strconv.Atoi(bSegments[i])
return an < bn
}
if aIsDigit != bIsDigit {
return aIsDigit
}
return aSegments[i] < bSegments[i]
}
return len(aSegments) < len(bSegments)
}
func orderDirsFirst(items *[]Item) {
sort.SliceStable(*items, func(i, j int) bool {
if (*items)[i].IsDir && !(*items)[j].IsDir {
return true
}
if !(*items)[i].IsDir && (*items)[j].IsDir {
return false
}
return false
})
}