Add support for sort by boolean
authorTom <tomaviv57@gmail.com>
Sat, 2 May 2020 09:57:34 +0000 (12:57 +0300)
committerGitHub <noreply@github.com>
Sat, 2 May 2020 09:57:34 +0000 (11:57 +0200)
tpl/collections/sort_test.go
tpl/compare/compare.go

index 2bf6e85fe214c4c05b8f68f3997d69368c8dbe88..75e23fc7ba4ab12a869c8f1edb974dc90927048e 100644 (file)
@@ -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},
index 50dafae3c2a35b8fcface91a7fec1d19cbbe6789..8ce5722736991ba1a95441f03421f0533e05fdfd 100644 (file)
@@ -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 {