diff --git a/internal/collection/collection.go b/internal/collection/collection.go index 481d6752..de9754a2 100644 --- a/internal/collection/collection.go +++ b/internal/collection/collection.go @@ -2,6 +2,7 @@ package collection import ( "runtime" + "sort" "github.com/tidwall/btree" "github.com/tidwall/geoindex" @@ -319,6 +320,7 @@ func (c *Collection) FieldArr() []string { for field, i := range c.fieldMap { arr[i] = field } + sort.Strings(arr) return arr } diff --git a/internal/server/crud.go b/internal/server/crud.go index e4e41355..d9d104ee 100644 --- a/internal/server/crud.go +++ b/internal/server/crud.go @@ -2,7 +2,6 @@ package server import ( "bytes" - "sort" "strconv" "strings" "time" @@ -23,22 +22,12 @@ type fvt struct { value float64 } -type byField []fvt - -func (a byField) Len() int { - return len(a) -} -func (a byField) Less(i, j int) bool { - return a[i].field < a[j].field -} -func (a byField) Swap(i, j int) { - a[i], a[j] = a[j], a[i] -} - -func orderFields(fmap map[string]int, fields []float64) []fvt { +func orderFields(fmap map[string]int, farr []string, fields []float64) []fvt { var fv fvt + var idx int fvs := make([]fvt, 0, len(fmap)) - for field, idx := range fmap { + for _, field := range farr { + idx = fmap[field] if idx < len(fields) { fv.field = field fv.value = fields[idx] @@ -47,7 +36,6 @@ func orderFields(fmap map[string]int, fields []float64) []fvt { } } } - sort.Sort(byField(fvs)) return fvs } func (server *Server) cmdBounds(msg *Message) (resp.Value, error) { @@ -255,7 +243,7 @@ func (server *Server) cmdGet(msg *Message) (resp.Value, error) { return NOMessage, errInvalidNumberOfArguments } if withfields { - fvs := orderFields(col.FieldMap(), fields) + fvs := orderFields(col.FieldMap(), col.FieldArr(), fields) if len(fvs) > 0 { fvals := make([]resp.Value, 0, len(fvs)*2) if msg.OutputType == JSON { diff --git a/internal/server/scanner.go b/internal/server/scanner.go index 55faa461..bfa6f176 100644 --- a/internal/server/scanner.go +++ b/internal/server/scanner.go @@ -485,7 +485,7 @@ func (sw *scanWriter) writeObject(opts ScanWriterParams) bool { } if sw.hasFieldsOutput() { - fvs := orderFields(sw.fmap, opts.fields) + fvs := orderFields(sw.fmap, sw.farr, opts.fields) if len(fvs) > 0 { fvals := make([]resp.Value, 0, len(fvs)*2) for i, fv := range fvs {