From 04b1a6d997e72d9abada28db22650d38ccbcbb39 Mon Sep 17 00:00:00 2001 From: Tom Date: Sat, 2 May 2020 12:57:34 +0300 Subject: [PATCH] Add support for sort by boolean --- tpl/collections/sort_test.go | 3 +++ tpl/compare/compare.go | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/tpl/collections/sort_test.go b/tpl/collections/sort_test.go index 2bf6e85fe..75e23fc7b 100644 --- a/tpl/collections/sort_test.go +++ b/tpl/collections/sort_test.go @@ -218,6 +218,9 @@ func TestSort(t *testing.T) { map[interface{}]interface{}{"Title": "Foo", "Weight": 10}, }, }, + // test boolean values + {[]bool{false, true, false}, "value", "asc", []bool{false, false, true}}, + {[]bool{false, true, false}, "value", "desc", []bool{true, false, false}}, // test error cases {(*[]TstX)(nil), nil, "asc", false}, {TstX{A: "a", B: "b"}, nil, "asc", false}, diff --git a/tpl/compare/compare.go b/tpl/compare/compare.go index 50dafae3c..8ce572273 100644 --- a/tpl/compare/compare.go +++ b/tpl/compare/compare.go @@ -252,6 +252,11 @@ func (ns *Namespace) compareGet(a interface{}, b interface{}) (float64, float64) case timeType: left = float64(toTimeUnix(av)) } + case reflect.Bool: + left = 0 + if av.Bool() { + left = 1 + } } bv := reflect.ValueOf(b) @@ -275,6 +280,11 @@ func (ns *Namespace) compareGet(a interface{}, b interface{}) (float64, float64) case timeType: right = float64(toTimeUnix(bv)) } + case reflect.Bool: + right = 0 + if bv.Bool() { + right = 1 + } } if ns.caseInsensitive && leftStr != nil && rightStr != nil {