From 974a482d1624c1bcf75681569974b5db7884d2fe Mon Sep 17 00:00:00 2001 From: Chanwit Kaewkasi Date: Thu, 6 Aug 2015 10:59:34 -0400 Subject: [PATCH] implemented naturalsorter for engine Signed-off-by: Chanwit Kaewkasi --- cluster/engine_sorter.go | 6 +++++- cluster/engine_sorter_test.go | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) 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") +}