This is a creation in Article, where the information may have evolved or changed.
Write code to find the need to sort Map[int64]int64, Golang in the sort package to solve the problem, the following is from the Internet to find 2 examples, Memo.
Example one:
package mainimport ( "fmt" "sort")func main() m := map[string]string{"b": "15", "z": "123123", "x": "sdf", "a": "12"} mk := make([]string, len(m)) i := 0 for k, _ := range m { mk[i] = k i++ } sort.SortStrings(mk) fmt.Println(mk)}
Example two:
//A data structure to hold A key/value pair.type pair struct {key string value int}//A slice of pair s that implements sort. Interface to sort by Value.Type pairlist []pairfunc (P pairlist) Swap (i, J int) {P[i], p[j] = P[j], p[i]}func (P Pa irlist) Len () int {return Len (P)}func (P pairlist) Less (i, J int) bool {return p[i]. Value < P[j]. Value}//a function to turn a map into a pairlist, then sort and return It.func Sortmapbyvalue (M map[string]int) pairlist {p: = Make (Pairlist, Len (m)) I: = 0 for k, V: = range m {p[i] = pair{k, v}} sort. Sort (P) return p}