package main
import (
"sort"
"fmt"
)
func main() {
ps := Persons{}
ps.persons = append(ps.persons, &Person{1, "mjy"})
ps.persons = append(ps.persons, &Person{3, "wxy"})
ps.persons = append(ps.persons, &Person{2, "zzy"})
ps.persons = append(ps.persons, &Person{5, "cyy"})
ps.persons = append(ps.persons, &Person{4, "wzh"})
ps.by = func(p, q *Person) bool {
return p.Id < q.Id
}
sort.Sort(ps)
for _, v := range ps.persons {
fmt.Println(v)
}
}
type Person struct {
Id int
Name string
}
type Persons struct {
persons []*Person
by func(p, q *Person) bool
}
func (this Persons) Len() int {
return len(this.persons)
}
func (this Persons) Less(i, j int) bool {
return this.by(this.persons[i], this.persons[j])
}
func (this Persons) Swap(i, j int) {
this.persons[i], this.persons[j] = this.persons[j], this.persons[i]
}