Skip to content

Commit 58ebf88

Browse files
FelixoidCivil
authored andcommitted
Use github.com/maruel/natural in metrics sorting
1 parent ac231fd commit 58ebf88

File tree

1 file changed

+2
-16
lines changed

1 file changed

+2
-16
lines changed

expr/helper/sort.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
package helper
22

33
import (
4-
"fmt"
5-
"regexp"
6-
"strconv"
7-
84
"github.com/go-graphite/carbonapi/expr/types"
5+
"github.com/maruel/natural"
96
)
107

118
// ByVals sorts by values
@@ -45,22 +42,11 @@ func (s ByName) Less(i, j int) bool { return s[i].Name < s[j].Name }
4542
// ByNameNatural sorts metric naturally by name
4643
type ByNameNatural []*types.MetricData
4744

48-
var dre = regexp.MustCompile(`\d+`)
49-
50-
func (s ByNameNatural) pad(str string) string {
51-
f := func(match []byte) []byte {
52-
n, _ := strconv.ParseInt(string(match), 10, 64)
53-
return []byte(fmt.Sprintf("%010d", n))
54-
}
55-
56-
return string(dre.ReplaceAllFunc([]byte(str), f))
57-
}
58-
5945
// Len returns length, required to be sortable
6046
func (s ByNameNatural) Len() int { return len(s) }
6147

6248
// Swap swaps to elements by IDs, required to be sortable
6349
func (s ByNameNatural) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
6450

6551
// Less compares two elements with specified IDs, required to be sortable
66-
func (s ByNameNatural) Less(i, j int) bool { return s.pad(s[i].Name) < s.pad(s[j].Name) }
52+
func (s ByNameNatural) Less(i, j int) bool { return natural.Less(s[i].Name, s[j].Name) }

0 commit comments

Comments
 (0)