в первую очередь недействителен тип json. Стоимость должна быть int
тип
попробуйте этот код
import (
"fmt"
"sort"
)
type Records []MyType
type MyType struct {
Job int `json:"Job"`
Cost int `json:"Cost"`
}
func (r Records) Len() int { return len(r) }
func (r Records) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
func (r Records) Less(i, j int) bool { return r[i].Cost > r[j].Cost }
func main() {
records := make(Records, 0, 1000)
records = append(records, MyType{Job: 100, Cost: 234})
records = append(records, MyType{Job: 101, Cost: 4000})
records = append(records, MyType{Job: 102, Cost: 700})
sort.Sort(records)
fmt.Println(records)
}
или без пользовательских ломтиков
type MyType struct {
Job int `json:"Job"`
Cost int `json:"Cost"`
}
func main() {
records := make([]MyType, 0, 1000)
records = append(records, MyType{Job: 100, Cost: 234})
records = append(records, MyType{Job: 101, Cost: 4000})
records = append(records, MyType{Job: 102, Cost: 700})
sort.Slice(records, func(i, j int) bool {
return records[i].Cost > records[j].Cost
})
fmt.Println(records)
}
выход
[{101 4000} {102 700} {100 234}]
ref https://golang.org/pkg/sort/#Slice