Brother Lian Blockchain training share go language simple sort

Source: Internet
Author: User
Tags key string

Due to the current user's lack of knowledge of the blockchain, coupled with the shortage of professionals in the blockchain and the lure of high salary, the market has spawned a number of blockchain training institutions. Brother Yun Jong, president of the Blockchain training Institute, says consumers need to be careful to differentiate themselves from the chain of training in the blockchain market.

The go language is optimized for programming multi-processor system applications, with go-compiled programs that are comparable to C or C + + code, and are more secure and support parallel processes.

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 Main

Import (

"FMT"

"Sort"

)

Func Main ()

M: = map[string]string{"B": "All", "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 Pairs that implements sort. Interface to sort by Value.

Type Pairlist []pair


Func (P pairlist) Swap (i, J int) {P[i], p[j] = P[j], P[i]}

Func (P pairlist) 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 maps 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.