Skip to content

Commit 974a482

Browse files
committed
implemented naturalsorter for engine
Signed-off-by: Chanwit Kaewkasi <[email protected]>
1 parent aae6fba commit 974a482

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

cluster/engine_sorter.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package cluster
22

3+
import (
4+
"github.com/skarademir/naturalsort"
5+
)
6+
37
// EngineSorter implements the Sort interface to sort Cluster.Engine.
48
// It is not guaranteed to be a stable sort.
59
type EngineSorter []*Engine
@@ -17,5 +21,5 @@ func (s EngineSorter) Swap(i, j int) {
1721
// Less reports whether the engine with index i should sort before the engine with index j.
1822
// Engines are sorted chronologically by name.
1923
func (s EngineSorter) Less(i, j int) bool {
20-
return s[i].Name < s[j].Name
24+
return naturalsort.NaturalSort([]string{s[i].Name, s[j].Name}).Less(0, 1)
2125
}

cluster/engine_sorter_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,17 @@ func TestEngineSorter(t *testing.T) {
1616
assert.Equal(t, engines[1].Name, "name2")
1717
assert.Equal(t, engines[2].Name, "name3")
1818
}
19+
20+
func TestNaturalSort(t *testing.T) {
21+
engines := []*Engine{{Name: "machine10"},
22+
{Name: "machine9"},
23+
{Name: "machine1"},
24+
{Name: "machine11"}}
25+
26+
sort.Sort(EngineSorter(engines))
27+
28+
assert.Equal(t, engines[0].Name, "machine1")
29+
assert.Equal(t, engines[1].Name, "machine9")
30+
assert.Equal(t, engines[2].Name, "machine10")
31+
assert.Equal(t, engines[3].Name, "machine11")
32+
}

0 commit comments

Comments
 (0)