-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort.go
48 lines (40 loc) · 1.37 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
package versions
import (
"sort"
)
// SortVersionStringSlice 对字符串形式的版本数组排序,返回值也是字符串形式的
func SortVersionStringSlice(versionStringSlice []string) []string {
versions := NewVersions(versionStringSlice...)
slice := SortVersionSlice(versions)
sortedVersionStringSlice := make([]string, 0)
for _, v := range slice {
sortedVersionStringSlice = append(sortedVersionStringSlice, v.Raw)
}
return sortedVersionStringSlice
}
// SortVersionSlice 对版本号排序
func SortVersionSlice(versions []*Version) []*Version {
// 先对所有的版本进行分组
groupMap := Group(versions)
// 对所有的分组排序
groupSlice := SortVersionGroupMap(groupMap)
// 收集所有的组,同时对组中的版本号也排序,保证最终收集到的版本号都是有序的
newVersions := make([]*Version, 0)
for _, g := range groupSlice {
newVersions = append(newVersions, g.SortVersions()...)
}
return newVersions
}
func SortVersionGroupMap(versionGroupMap map[string]*VersionGroup) []*VersionGroup {
groupSlice := make([]*VersionGroup, 0)
for _, g := range versionGroupMap {
groupSlice = append(groupSlice, g)
}
SortVersionGroupSlice(groupSlice)
return groupSlice
}
func SortVersionGroupSlice(groupSlice []*VersionGroup) {
sort.Slice(groupSlice, func(i, j int) bool {
return groupSlice[i].CompareTo(groupSlice[j]) < 0
})
}