Skip to content

Commit d27e5a3

Browse files
committed
Add support for float in sort() builtin
Fixes #448
1 parent 507d734 commit d27e5a3

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

builtin/builtin_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ import (
77
"testing"
88
"time"
99

10+
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
12+
1013
"github.com/antonmedv/expr"
1114
"github.com/antonmedv/expr/builtin"
1215
"github.com/antonmedv/expr/checker"
1316
"github.com/antonmedv/expr/conf"
1417
"github.com/antonmedv/expr/parser"
1518
"github.com/antonmedv/expr/test/mock"
16-
"github.com/stretchr/testify/assert"
17-
"github.com/stretchr/testify/require"
1819
)
1920

2021
func TestBuiltin(t *testing.T) {
@@ -403,6 +404,7 @@ func TestBuiltin_sort(t *testing.T) {
403404
env := map[string]any{
404405
"ArrayOfString": []string{"foo", "bar", "baz"},
405406
"ArrayOfInt": []int{3, 2, 1},
407+
"ArrayOfFloat": []float64{3.0, 2.0, 1.0},
406408
"ArrayOfFoo": []mock.Foo{{Value: "c"}, {Value: "a"}, {Value: "b"}},
407409
}
408410
tests := []struct {
@@ -411,6 +413,7 @@ func TestBuiltin_sort(t *testing.T) {
411413
}{
412414
{`sort([])`, []any{}},
413415
{`sort(ArrayOfInt)`, []any{1, 2, 3}},
416+
{`sort(ArrayOfFloat)`, []any{1.0, 2.0, 3.0}},
414417
{`sort(ArrayOfInt, 'desc')`, []any{3, 2, 1}},
415418
{`sortBy(ArrayOfFoo, 'Value')`, []any{mock.Foo{Value: "a"}, mock.Foo{Value: "b"}, mock.Foo{Value: "c"}}},
416419
{`sortBy([{id: "a"}, {id: "b"}], "id", "desc")`, []any{map[string]any{"id": "b"}, map[string]any{"id": "a"}}},

builtin/sort.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ func (s *Sortable) Less(i, j int) bool {
3333
return a.Int() > b.Int()
3434
}
3535
return a.Int() < b.Int()
36+
case reflect.Float64, reflect.Float32:
37+
if s.Desc {
38+
return a.Float() > b.Float()
39+
}
40+
return a.Float() < b.Float()
3641
case reflect.String:
3742
if s.Desc {
3843
return a.String() > b.String()

0 commit comments

Comments
 (0)