|
1 | 1 | package helper
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "fmt" |
5 |
| - "regexp" |
6 |
| - "strconv" |
7 |
| - |
8 | 4 | "github.com/go-graphite/carbonapi/expr/types"
|
| 5 | + "github.com/maruel/natural" |
9 | 6 | )
|
10 | 7 |
|
11 | 8 | // ByVals sorts by values
|
@@ -45,22 +42,11 @@ func (s ByName) Less(i, j int) bool { return s[i].Name < s[j].Name }
|
45 | 42 | // ByNameNatural sorts metric naturally by name
|
46 | 43 | type ByNameNatural []*types.MetricData
|
47 | 44 |
|
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 |
| - |
59 | 45 | // Len returns length, required to be sortable
|
60 | 46 | func (s ByNameNatural) Len() int { return len(s) }
|
61 | 47 |
|
62 | 48 | // Swap swaps to elements by IDs, required to be sortable
|
63 | 49 | func (s ByNameNatural) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
64 | 50 |
|
65 | 51 | // 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