-
-
Save nerdyworm/1bd277c8af51f89dd2ec to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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