Adding ability to sort results based on different keys
This commit is contained in:
parent
a65ca4fbe9
commit
19e897ae3f
@ -50,7 +50,10 @@ func executeQuery(rw http.ResponseWriter, req *http.Request) {
|
|||||||
|
|
||||||
entries := getRecords(queryContext{geo, request.Profile, request.WalkingDist})
|
entries := getRecords(queryContext{geo, request.Profile, request.WalkingDist})
|
||||||
features := fixFeatures(request.Features)
|
features := fixFeatures(request.Features)
|
||||||
|
|
||||||
foundEntries := findRecords(entries, features, request.MinScore)
|
foundEntries := findRecords(entries, features, request.MinScore)
|
||||||
|
sorter := recordSorter{entries: foundEntries, key: request.SortKey, ascending: request.SortAscending}
|
||||||
|
sorter.sort()
|
||||||
|
|
||||||
response := jsonQueryResponse{
|
response := jsonQueryResponse{
|
||||||
Count: len(foundEntries),
|
Count: len(foundEntries),
|
||||||
|
62
types.go
62
types.go
@ -22,6 +22,8 @@
|
|||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
|
import "sort"
|
||||||
|
|
||||||
type featureMap map[string]float64
|
type featureMap map[string]float64
|
||||||
|
|
||||||
type jsonAccessRequest struct {
|
type jsonAccessRequest struct {
|
||||||
@ -35,13 +37,15 @@ type jsonGeoData struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type jsonQueryRequest struct {
|
type jsonQueryRequest struct {
|
||||||
Features featureMap `json:"features"`
|
Features featureMap `json:"features"`
|
||||||
Geo *jsonGeoData `json:"geo"`
|
Geo *jsonGeoData `json:"geo"`
|
||||||
HintSteps int `json:"hintSteps"`
|
HintSteps int `json:"hintSteps"`
|
||||||
MaxResults int `json:"maxResults"`
|
MaxResults int `json:"maxResults"`
|
||||||
MinScore float64 `json:"minScore"`
|
MinScore float64 `json:"minScore"`
|
||||||
Profile featureMap `json:"profile"`
|
Profile featureMap `json:"profile"`
|
||||||
WalkingDist float64 `json:"walkingDist"`
|
SortAscending bool `json:"SortAscending"`
|
||||||
|
SortKey string `json:"sortKey"`
|
||||||
|
WalkingDist float64 `json:"walkingDist"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type jsonColumn struct {
|
type jsonColumn struct {
|
||||||
@ -130,14 +134,46 @@ type record struct {
|
|||||||
|
|
||||||
type records []record
|
type records []record
|
||||||
|
|
||||||
func (slice records) Len() int {
|
type recordSorter struct {
|
||||||
return len(slice)
|
ascending bool
|
||||||
|
entries records
|
||||||
|
key string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slice records) Less(i, j int) bool {
|
func (sorter recordSorter) sort() {
|
||||||
return slice[i].score > slice[j].score
|
if sorter.ascending {
|
||||||
|
sort.Sort(sorter)
|
||||||
|
} else {
|
||||||
|
sort.Sort(sort.Reverse(sorter))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slice records) Swap(i, j int) {
|
func (sorter recordSorter) Len() int {
|
||||||
slice[i], slice[j] = slice[j], slice[i]
|
return len(sorter.entries)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sorter recordSorter) Less(i, j int) bool {
|
||||||
|
entry1 := sorter.entries[i]
|
||||||
|
entry2 := sorter.entries[j]
|
||||||
|
|
||||||
|
switch sorter.key {
|
||||||
|
case "accessCount":
|
||||||
|
return entry1.accessCount < entry2.accessCount
|
||||||
|
case "closestStn":
|
||||||
|
return entry1.closestStn < entry2.closestStn
|
||||||
|
case "compatibility":
|
||||||
|
return entry1.compatibility < entry2.compatibility
|
||||||
|
case "distanceToStn":
|
||||||
|
return entry1.distanceToStn < entry2.distanceToStn
|
||||||
|
case "distanceToUser":
|
||||||
|
return entry1.distanceToUser < entry2.distanceToUser
|
||||||
|
case "name":
|
||||||
|
return entry1.name < entry2.name
|
||||||
|
default:
|
||||||
|
return entry1.score < entry2.score
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sorter recordSorter) Swap(i, j int) {
|
||||||
|
sorter.entries[i], sorter.entries[j] = sorter.entries[j], sorter.entries[i]
|
||||||
}
|
}
|
||||||
|
2
util.go
2
util.go
@ -26,7 +26,6 @@ import (
|
|||||||
"github.com/kellydunn/golang-geo"
|
"github.com/kellydunn/golang-geo"
|
||||||
"log"
|
"log"
|
||||||
"math"
|
"math"
|
||||||
"sort"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -97,7 +96,6 @@ func findRecords(entries records, features featureMap, minScore float64) records
|
|||||||
foundEntries = append(foundEntries, entry)
|
foundEntries = append(foundEntries, entry)
|
||||||
})
|
})
|
||||||
|
|
||||||
sort.Sort(foundEntries)
|
|
||||||
return foundEntries
|
return foundEntries
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user