Skip to content

Commit

Permalink
Add support for sort by boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
Mipsters committed May 2, 2020
1 parent dd31e80 commit 04b1a6d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
3 changes: 3 additions & 0 deletions tpl/collections/sort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
10 changes: 10 additions & 0 deletions tpl/compare/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down

0 comments on commit 04b1a6d

Please sign in to comment.