Skip to content

Instantly share code, notes, and snippets.

@yomusu
Created October 31, 2014 08:50
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 yomusu/c44218f34c1f896180cb to your computer and use it in GitHub Desktop.
Save yomusu/c44218f34c1f896180cb to your computer and use it in GitHub Desktop.
Go sort map
package main
import (
"fmt"
"sort"
)
type Shop struct {
Code string
}
type Shops []*Shop
func (d Shops) Len() int {
return len(d)
}
func (d Shops) Swap(i, j int) {
d[i], d[j] = d[j], d[i]
}
func (d Shops) Less(i, j int) bool {
return d[i].Code < d[j].Code
}
func main() {
shopMap := make(map[string]*Shop)
shopMap["xx"] = &Shop{Code: "xx"}
shopMap["hoge"] = &Shop{Code: "hoge"}
shopMap["fuga"] = &Shop{Code: "fuga"}
var keys Shops
for _, s := range shopMap {
keys = append(keys, s)
}
sort.Sort(keys)
fmt.Printf("%t", keys)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment