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