implemented naturalsorter for engine

Signed-off-by: Chanwit Kaewkasi <chanwit@gmail.com>
This commit is contained in:
Chanwit Kaewkasi 2015-08-06 10:59:34 -04:00
parent aae6fba936
commit 974a482d16
2 changed files with 19 additions and 1 deletions

View File

@ -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)
}

View File

@ -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")
}