Go Simple sort

Source: Internet
Author: User
Tags key string
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}  
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.