Skip to content

Commit ceed5b5

Browse files
committed
use slices.Sort instead of old Sort interface
Benchmark confirms that slices.Sort is faster.
1 parent b11b81e commit ceed5b5

File tree

3 files changed

+8
-7
lines changed

3 files changed

+8
-7
lines changed

cidenc/encoder.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ func (enc Encoder) noopRecode(v string) (bool, error) {
5353
func cidVer(v string) int {
5454
if len(v) == 46 && v[:2] == "Qm" {
5555
return 0
56-
} else {
57-
return 1
5856
}
57+
return 1
5958
}

format.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,8 @@ type FormatStringError struct {
115115
func (e FormatStringError) Error() string {
116116
if e.Specifier == "" {
117117
return e.Message
118-
} else {
119-
return fmt.Sprintf("%s: %s", e.Message, e.Specifier)
120118
}
119+
return fmt.Sprintf("%s: %s", e.Message, e.Specifier)
121120
}
122121

123122
func baseToString(base mb.Encoding) string {

slice.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package cidutil
22

33
import (
4-
"sort"
4+
"slices"
5+
"strings"
56

67
"github.com/ipfs/go-cid"
78
)
@@ -22,10 +23,12 @@ func (s Slice) Swap(i, j int) {
2223
}
2324

2425
func (s Slice) Sort() {
25-
sort.Sort(s)
26+
Sort(s)
2627
}
2728

2829
// Sort sorts a slice of CIDs
2930
func Sort(s []cid.Cid) {
30-
Slice(s).Sort()
31+
slices.SortFunc(s, func(a, b cid.Cid) int {
32+
return strings.Compare(a.KeyString(), b.KeyString())
33+
})
3134
}

0 commit comments

Comments
 (0)