Skip to content

Commit c227603

Browse files
authored
Merge pull request #2065 from CortexFoundation/dev
p2p: use package slices to sort in PeersInfo
2 parents d34f9af + 6a69b16 commit c227603

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

p2p/server.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package p2p
1919

2020
import (
2121
"bytes"
22+
"cmp"
2223
"crypto/ecdsa"
2324
"encoding/hex"
2425
"errors"
@@ -1140,12 +1141,9 @@ func (srv *Server) PeersInfo() []*PeerInfo {
11401141
}
11411142
}
11421143
// Sort the result array alphabetically by node identifier
1143-
for i := 0; i < len(infos); i++ {
1144-
for j := i + 1; j < len(infos); j++ {
1145-
if infos[i].ID > infos[j].ID {
1146-
infos[i], infos[j] = infos[j], infos[i]
1147-
}
1148-
}
1149-
}
1144+
slices.SortFunc(infos, func(a, b *PeerInfo) int {
1145+
return cmp.Compare(a.ID, b.ID)
1146+
})
1147+
11501148
return infos
11511149
}

0 commit comments

Comments
 (0)