Skip to content

Instantly share code, notes, and snippets.

@nerdyworm
Last active September 5, 2017 23:31
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nerdyworm/1bd277c8af51f89dd2ec to your computer and use it in GitHub Desktop.
Save nerdyworm/1bd277c8af51f89dd2ec to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"sort"
)
type Country struct {
Name string
}
type Countries []Country
func (slice Countries) Len() int {
return len(slice)
}
func (slice Countries) Less(i, j int) bool {
return slice[i].Name < slice[j].Name;
}
func (slice Countries) Swap(i, j int) {
slice[i], slice[j] = slice[j], slice[i]
}
func main() {
countries := Countries{
{Name: "United States"},
{Name: "Bahamas"},
{Name: "Japan"},
}
sort.Sort(countries)
for i, c := range countries {
fmt.Println(i, c.Name)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment