p2p: use package slices to sort in PeersInfo #29957 (#1700)

This commit is contained in:
Daniel Liu 2025-12-07 18:12:51 +08:00 committed by GitHub
parent c922f26d0c
commit 0ad629d484
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -18,9 +18,11 @@
package p2p
import (
"cmp"
"crypto/ecdsa"
"errors"
"net"
"slices"
"sync"
"sync/atomic"
"time"
@ -1046,12 +1048,9 @@ func (srv *Server) PeersInfo() []*PeerInfo {
}
}
// Sort the result array alphabetically by node identifier
for i := 0; i < len(infos); i++ {
for j := i + 1; j < len(infos); j++ {
if infos[i].ID > infos[j].ID {
infos[i], infos[j] = infos[j], infos[i]
}
}
}
slices.SortFunc(infos, func(a, b *PeerInfo) int {
return cmp.Compare(a.ID, b.ID)
})
return infos
}