all: use cmp.Compare (#30958)

This commit is contained in:
gitglorythegreat 2025-01-02 21:06:47 +08:00 committed by GitHub
parent 0feb999d3f
commit 85ffbde427
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 10 additions and 35 deletions

View File

@ -17,6 +17,7 @@
package main
import (
"cmp"
"context"
"errors"
"fmt"
@ -292,13 +293,7 @@ func sortChanges(changes []types.Change) {
if a.Action == b.Action {
return strings.Compare(*a.ResourceRecordSet.Name, *b.ResourceRecordSet.Name)
}
if score[string(a.Action)] < score[string(b.Action)] {
return -1
}
if score[string(a.Action)] > score[string(b.Action)] {
return 1
}
return 0
return cmp.Compare(score[string(a.Action)], score[string(b.Action)])
})
}

View File

@ -18,6 +18,7 @@ package main
import (
"bytes"
"cmp"
"encoding/json"
"fmt"
"os"
@ -104,13 +105,7 @@ func (ns nodeSet) topN(n int) nodeSet {
byscore = append(byscore, v)
}
slices.SortFunc(byscore, func(a, b nodeJSON) int {
if a.Score > b.Score {
return -1
}
if a.Score < b.Score {
return 1
}
return 0
return cmp.Compare(b.Score, a.Score)
})
result := make(nodeSet, n)
for _, v := range byscore[:n] {

View File

@ -18,6 +18,7 @@ package snapshot
import (
"bytes"
"cmp"
"fmt"
"slices"
"sort"
@ -45,13 +46,7 @@ func (it *weightedIterator) Cmp(other *weightedIterator) int {
return 1
}
// Same account/storage-slot in multiple layers, split by priority
if it.priority < other.priority {
return -1
}
if it.priority > other.priority {
return 1
}
return 0
return cmp.Compare(it.priority, other.priority)
}
// fastIterator is a more optimized multi-layer iterator which maintains a

View File

@ -17,6 +17,7 @@
package p2p
import (
"cmp"
"fmt"
"strings"
@ -81,13 +82,7 @@ func (cap Cap) String() string {
// Cmp defines the canonical sorting order of capabilities.
func (cap Cap) Cmp(other Cap) int {
if cap.Name == other.Name {
if cap.Version < other.Version {
return -1
}
if cap.Version > other.Version {
return 1
}
return 0
return cmp.Compare(cap.Version, other.Version)
}
return strings.Compare(cap.Name, other.Name)
}

View File

@ -18,6 +18,7 @@ package pathdb
import (
"bytes"
"cmp"
"fmt"
"slices"
"sort"
@ -45,13 +46,7 @@ func (it *weightedIterator) Cmp(other *weightedIterator) int {
return 1
}
// Same account/storage-slot in multiple layers, split by priority
if it.priority < other.priority {
return -1
}
if it.priority > other.priority {
return 1
}
return 0
return cmp.Compare(it.priority, other.priority)
}
// fastIterator is a more optimized multi-layer iterator which maintains a