diff --git a/cluster/engine_sorter.go b/cluster/engine_sorter.go index 4ddebe6ec4..e468f441e1 100644 --- a/cluster/engine_sorter.go +++ b/cluster/engine_sorter.go @@ -1,5 +1,9 @@ package cluster +import ( + "github.com/skarademir/naturalsort" +) + // EngineSorter implements the Sort interface to sort Cluster.Engine. // It is not guaranteed to be a stable sort. type EngineSorter []*Engine @@ -17,5 +21,5 @@ func (s EngineSorter) Swap(i, j int) { // Less reports whether the engine with index i should sort before the engine with index j. // Engines are sorted chronologically by name. func (s EngineSorter) Less(i, j int) bool { - return s[i].Name < s[j].Name + return naturalsort.NaturalSort([]string{s[i].Name, s[j].Name}).Less(0, 1) } diff --git a/cluster/engine_sorter_test.go b/cluster/engine_sorter_test.go index 56f8de24ad..5973fc6000 100644 --- a/cluster/engine_sorter_test.go +++ b/cluster/engine_sorter_test.go @@ -16,3 +16,17 @@ func TestEngineSorter(t *testing.T) { assert.Equal(t, engines[1].Name, "name2") assert.Equal(t, engines[2].Name, "name3") } + +func TestNaturalSort(t *testing.T) { + engines := []*Engine{{Name: "machine10"}, + {Name: "machine9"}, + {Name: "machine1"}, + {Name: "machine11"}} + + sort.Sort(EngineSorter(engines)) + + assert.Equal(t, engines[0].Name, "machine1") + assert.Equal(t, engines[1].Name, "machine9") + assert.Equal(t, engines[2].Name, "machine10") + assert.Equal(t, engines[3].Name, "machine11") +}