@@ -3,8 +3,10 @@ package metrics
33import (
44 "fmt"
55 "io"
6- "sort "
6+ "strings "
77 "time"
8+
9+ "golang.org/x/exp/slices"
810)
911
1012// Write sorts writes each metric in the given registry periodically to the
@@ -18,12 +20,11 @@ func Write(r Registry, d time.Duration, w io.Writer) {
1820// WriteOnce sorts and writes metrics in the given registry to the given
1921// io.Writer.
2022func WriteOnce (r Registry , w io.Writer ) {
21- var namedMetrics namedMetricSlice
23+ var namedMetrics [] namedMetric
2224 r .Each (func (name string , i interface {}) {
2325 namedMetrics = append (namedMetrics , namedMetric {name , i })
2426 })
25-
26- sort .Sort (namedMetrics )
27+ slices .SortFunc (namedMetrics , namedMetric .cmp )
2728 for _ , namedMetric := range namedMetrics {
2829 switch metric := namedMetric .m .(type ) {
2930 case Counter :
@@ -91,13 +92,6 @@ type namedMetric struct {
9192 m interface {}
9293}
9394
94- // namedMetricSlice is a slice of namedMetrics that implements sort.Interface.
95- type namedMetricSlice []namedMetric
96-
97- func (nms namedMetricSlice ) Len () int { return len (nms ) }
98-
99- func (nms namedMetricSlice ) Swap (i , j int ) { nms [i ], nms [j ] = nms [j ], nms [i ] }
100-
101- func (nms namedMetricSlice ) Less (i , j int ) bool {
102- return nms [i ].name < nms [j ].name
95+ func (m namedMetric ) cmp (other namedMetric ) int {
96+ return strings .Compare (m .name , other .name )
10397}
0 commit comments