-
Notifications
You must be signed in to change notification settings - Fork 121
/
sorter.go
53 lines (42 loc) · 1.09 KB
/
sorter.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
package main
import (
"log"
"sort"
"strconv"
"strings"
"unicode"
)
// Almost completely ripped off https://www.socketloop.com/tutorials/golang-natural-string-sorting-example
type Compare func(str1, str2 string) bool
func (cmp Compare) Sort(strs []string) {
strSort := &strSorter{
strs: strs,
cmp: cmp,
}
sort.Sort(strSort)
}
type strSorter struct {
strs []string
cmp func(str1, str2 string) bool
}
func extractNumberFromString(str string) (num int) {
strSlice := make([]string, 0)
for _, v := range str {
if unicode.IsDigit(v) {
strSlice = append(strSlice, string(v))
}
}
// If the tag was all non-digits, the strSlice would be empty (e.g., 'latest')
// therefore just throw it to the end (1 << 32 is maxint)
if len(strSlice) == 0 {
return 1 << 32
}
num, err := strconv.Atoi(strings.Join(strSlice, ""))
if err != nil {
log.Fatal(err)
}
return num
}
func (s *strSorter) Len() int { return len(s.strs) }
func (s *strSorter) Swap(i, j int) { s.strs[i], s.strs[j] = s.strs[j], s.strs[i] }
func (s *strSorter) Less(i, j int) bool { return s.cmp(s.strs[i], s.strs[j]) }