added sort support
This commit is contained in:
parent
558d6ae7d0
commit
1674abae73
5 changed files with 377 additions and 66 deletions
31
helper.go
31
helper.go
|
|
@ -12,6 +12,37 @@ const (
|
|||
anys = string(any)
|
||||
)
|
||||
|
||||
type less func(i, j int) bool
|
||||
|
||||
func reverse(f less) less {
|
||||
return func(i, j int) bool {
|
||||
return f(j, i)
|
||||
}
|
||||
}
|
||||
|
||||
func index(f Filter, i int, fields []string) (val reflect.Value) {
|
||||
val = reflect.ValueOf(f.Index(i)).FieldByName(fields[0])
|
||||
for j := 1; j < len(fields); j++ {
|
||||
val = val.FieldByName(fields[j])
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func makeLess(f Filter, fields []string) less {
|
||||
switch index(f, 0, fields).Kind() {
|
||||
case reflect.Int, reflect.Int8, reflect.Int16,
|
||||
reflect.Int32, reflect.Int64:
|
||||
return func(i, j int) bool { return index(f, i, fields).Int() < index(f, j, fields).Int() }
|
||||
case reflect.Float32, reflect.Float64:
|
||||
return func(i, j int) bool { return index(f, i, fields).Float() < index(f, j, fields).Float() }
|
||||
case reflect.String:
|
||||
return func(i, j int) bool { return index(f, i, fields).String() < index(f, j, fields).String() }
|
||||
case reflect.Bool:
|
||||
return func(i, _ int) bool { return index(f, i, fields).Bool() }
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func getValue(v reflect.Value) (s string, b bool, err error) {
|
||||
switch v.Kind() {
|
||||
case reflect.Int, reflect.Int8, reflect.Int16,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue