diff options
| author | Anaminus <[email protected]> | 2015-02-19 07:06:59 -0600 |
|---|---|---|
| committer | Dave Collins <[email protected]> | 2015-02-23 15:16:52 -0600 |
| commit | a4b53deb9bf78b69198092ddafa308d3be2bd002 (patch) | |
| tree | d6e98ef0bc64c1cb4134dc51582851ef9336fd65 | |
| parent | 1aaf839fb07e099361e445273993ccd9adc21b07 (diff) | |
Add support for sorting keys that are arrays.
| -rw-r--r-- | spew/common.go | 40 | ||||
| -rw-r--r-- | spew/common_test.go | 5 |
2 files changed, 34 insertions, 11 deletions
diff --git a/spew/common.go b/spew/common.go index 0ce0df1..81fac0b 100644 --- a/spew/common.go +++ b/spew/common.go @@ -340,24 +340,42 @@ func (s *valuesSorter) Swap(i, j int) { s.values[i], s.values[j] = s.values[j], s.values[i] } -// Less returns whether the value at index i should sort before the -// value at index j. It is part of the sort.Interface implementation. -func (s *valuesSorter) Less(i, j int) bool { - switch s.values[i].Kind() { +// valueSortLess returns whether the first value should sort before the second +// value. It is used by valueSorter.Less as part of the sort.Interface +// implementation. +func valueSortLess(a, b reflect.Value) bool { + switch a.Kind() { case reflect.Bool: - return !s.values[i].Bool() && s.values[j].Bool() + return !a.Bool() && b.Bool() case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int: - return s.values[i].Int() < s.values[j].Int() + return a.Int() < b.Int() case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint: - return s.values[i].Uint() < s.values[j].Uint() + return a.Uint() < b.Uint() case reflect.Float32, reflect.Float64: - return s.values[i].Float() < s.values[j].Float() + return a.Float() < b.Float() case reflect.String: - return s.values[i].String() < s.values[j].String() + return a.String() < b.String() case reflect.Uintptr: - return s.values[i].Uint() < s.values[j].Uint() + return a.Uint() < b.Uint() + case reflect.Array: + // Compare the contents of both arrays. + l := a.Len() + for i := 0; i < l; i++ { + av := a.Index(i) + bv := b.Index(i) + if av.Interface() == bv.Interface() { + continue + } + return valueSortLess(av, bv) + } } - return s.values[i].String() < s.values[j].String() + return a.String() < b.String() +} + +// Less returns whether the value at index i should sort before the +// value at index j. It is part of the sort.Interface implementation. +func (s *valuesSorter) Less(i, j int) bool { + return valueSortLess(s.values[i], s.values[j]) } // sortValues is a generic sort function for native types: int, uint, bool, diff --git a/spew/common_test.go b/spew/common_test.go index 3bea81f..8e741cd 100644 --- a/spew/common_test.go +++ b/spew/common_test.go @@ -166,6 +166,11 @@ func TestSortValues(t *testing.T) { []reflect.Value{b, a, c}, []reflect.Value{a, b, c}, }, + // Array + { + []reflect.Value{v([3]int{3, 2, 1}), v([3]int{1, 3, 2}), v([3]int{1, 2, 3})}, + []reflect.Value{v([3]int{1, 2, 3}), v([3]int{1, 3, 2}), v([3]int{3, 2, 1})}, + }, // Uintptrs. { []reflect.Value{v(uintptr(2)), v(uintptr(1)), v(uintptr(3))}, |
