Skip to content

Instantly share code, notes, and snippets.

@delba
Created March 5, 2015 16:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save delba/fc05d4c368d694e69310 to your computer and use it in GitHub Desktop.
Save delba/fc05d4c368d694e69310 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"sort"
)
type StarredRepository struct {
Repository string
Users []string
}
type ByPopularity []StarredRepository
func (c ByPopularity) Len() int {
return len(c)
}
func (c ByPopularity) Swap(i, j int) {
c[i], c[j] = c[j], c[i]
}
func (c ByPopularity) Less(i, j int) bool {
return len(c[i].Users) > len(c[j].Users)
}
func main() {
rails := StarredRepository{
Repository: "rails",
Users: []string{"damien", "arnaud", "noemie"},
}
jquery := StarredRepository{
Repository: "jquery",
Users: []string{"arnaud", "noemie"},
}
golang := StarredRepository{
Repository: "golang",
Users: []string{"damien"},
}
starredRepositories := []StarredRepository{jquery, rails, golang}
fmt.Printf("%+v\n", starredRepositories)
// [{Repository:jquery Users:[arnaud noemie]} {Repository:rails Users:[damien arnaud noemie]} {Repository:golang Users:[damien]}]
sort.Sort(ByPopularity(starredRepositories))
fmt.Printf("%+v\n", starredRepositories)
// [{Repository:rails Users:[damien arnaud noemie]} {Repository:jquery Users:[arnaud noemie]} {Repository:golang Users:[damien]}]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment