Reputation: 197
I am attempting to have two variations of the sort method: one form which sorts the elements by name, and another that sorts the elements by salary. sort.Sort(people(data)) works when my less method compares whatever.salary. It also works if I change it to whatever.name. I would like to be able to specifically call both of these options in the less method as shown in the below code. My logic is using sort.Sort(people(data.name)) for name, and sort.Sort(people(data.salary)) for salary. These are not working. Can this even be done?
package main
import (
"fmt"
"sort"
)
type Comparable interface {
Len()
Less(i, j int) bool
Swap(i, j int)
}
type person struct {
name string
salary float64
}
func (a person) String() string {
return fmt.Sprintf("%s: %g \n", a.name, a.salary)
}
type people []*person
func (a people) Len() int {
return len(a)
}
func (a people) Less(i, j int) bool {
return a[i].salary < a[j].salary
return a[i].name < a[j].name
}
func (a people) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}
func main() {
var data = make(people, 10)
var a, b, c, d, e, f, g, h, i, j person
a.name, b.name, c.name, d.name, e.name, f.name,
g.name, h.name, i.name, j.name = "Sheila Broflovski", "Ben Affleck",
"Mr. Hankey", "Stan Marsh", "Kyle Broflovski", "Eric Cartman",
"Kenny McCormick", "Mr. Garrison", "Matt Stone", "Trey Parker"
a.salary, b.salary, c.salary, d.salary, e.salary, f.salary,
g.salary, h.salary, i.salary, j.salary = 82000, 74000, 0, 400,
2500, 1000, 4, 34000, 234000, 234000
a.salary = 82000
data[0] = &a
data[1] = &b
data[2] = &c
data[3] = &d
data[4] = &e
data[5] = &f
data[6] = &g
data[7] = &h
data[8] = &i
data[9] = &j
fmt.Println("\n\n\n")
fmt.Print(data)
sort.Sort(people(data)) //This works even with the two return statements
sort.Sort(people(data.name)) //This does not work. Exist, a version that does?
sort.Sort(people(data.salary)) //This does not work. Exist, a version that does?
fmt.Println("\n\n\n")
fmt.Print(data)
}
Upvotes: 1
Views: 79
Reputation: 43899
To implement a second sort ordering with the standard library sort
package, you will need to define a helper type that implements sort.Interface
. To handle the salary case in your example, you could do something like the following:
type bySalary struct {
people
}
func (s bySalary) Less(i, j int) bool {
return s.people[i].salary < s.people[j].salary
}
Here, the Len
and Swap
methods required by sort.Interface
come from the embedded people
slice, while I've provided a replacement comparison operation. To sort a people
array, you can now call:
sort.Sort(bySalary{data})
Using this pattern, it is easy to implement as many additional sort keys as you need with very little duplicate code.
You can play with this example here: http://play.golang.org/p/kq3SuXMylT
Upvotes: 1
Reputation: 58201
A common way to introduce the methods for sorting is to use a new type that describes the sorting condition. That's byName
and bySalary
here. Then you can sort using sort.Sort(byName(data))
.
Here's some demonstration code. Go also has really great good for constructing data-structures (used prolifically, for example, in table-driven tests), which also can help construct your people data here.
package main
import "fmt"
import "sort"
type person struct {
Name string
Salary float64
}
type people []*person
type byName people
type bySalary people
func (p byName) Len() int { return len(p) }
func (p byName) Less(i, j int) bool { return p[i].Name < p[j].Name }
func (p byName) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p bySalary) Len() int { return len(p) }
func (p bySalary) Less(i, j int) bool { return p[i].Salary < p[j].Salary }
func (p bySalary) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func main() {
p := people{
{"Sheila Broflovski", 82000},
{"Ben Affleck", 74000},
{"Mr. Hankey", 0},
{"Stan Marsh", 400},
{"Kyle Broflovski", 2500},
{"Eric Cartman", 1000},
{"Kenny McCormick", 4},
{"Mr. Garrison", 34000},
{"Matt Stone", 234000},
{"Trey Parker", 234000},
}
fmt.Println("by name")
sort.Sort(byName(p))
for _, x := range p {
fmt.Println(*x)
}
fmt.Println("by salary")
sort.Sort(bySalary(p))
for _, x := range p {
fmt.Println(*x)
}
}
Upvotes: 2