Add support for sort by boolean

This commit is contained in:
Tom 2020-05-02 12:57:34 +03:00 committed by GitHub
parent dd31e80007
commit 04b1a6d997
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 0 deletions

View file

@ -218,6 +218,9 @@ func TestSort(t *testing.T) {
map[interface{}]interface{}{"Title": "Foo", "Weight": 10}, 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 // test error cases
{(*[]TstX)(nil), nil, "asc", false}, {(*[]TstX)(nil), nil, "asc", false},
{TstX{A: "a", B: "b"}, nil, "asc", false}, {TstX{A: "a", B: "b"}, nil, "asc", false},

View file

@ -252,6 +252,11 @@ func (ns *Namespace) compareGet(a interface{}, b interface{}) (float64, float64)
case timeType: case timeType:
left = float64(toTimeUnix(av)) left = float64(toTimeUnix(av))
} }
case reflect.Bool:
left = 0
if av.Bool() {
left = 1
}
} }
bv := reflect.ValueOf(b) bv := reflect.ValueOf(b)
@ -275,6 +280,11 @@ func (ns *Namespace) compareGet(a interface{}, b interface{}) (float64, float64)
case timeType: case timeType:
right = float64(toTimeUnix(bv)) right = float64(toTimeUnix(bv))
} }
case reflect.Bool:
right = 0
if bv.Bool() {
right = 1
}
} }
if ns.caseInsensitive && leftStr != nil && rightStr != nil { if ns.caseInsensitive && leftStr != nil && rightStr != nil {