Go basics-Sort and find operations

Source: Internet
Author: User

The sort operations are mostly in the sort package, and the import is ready to use the
Import ("Sort")

Common operations

Sort. Ints: Sorting integers
Sort. Strings: Sorting strings
Sort. float64s: Sorting floating-point numbers

Examples of Use:

Package Mainimport (    "sort"    "FMT") Func main () {    // sort    integers = []int {2,3,5,10,20,1,0}    sort. Ints (s)    FMT. Println (s)    // sort    string = []string{"cc", "yy", "dd", "EE", "zz"} sort    . Strings (b)    FMT. Println (b)    // sort    The floating-point number = []float64{9.1,1.2,3,2,1.9,2.2,10.1}    . float64s (d)    FMT. Println (d)}

There are also the following common find operations

Sort. Searchints (a[]int,b int): Find the index position of b from integer slice a
Sort. searchfloat64s (a[]float64,b float64): Find the index position of b from a floating-point slice
Sort. Strings (A[]strings,b String): Find the index position of B from a character slice

Examples of use are as follows:

Package Mainimport (    "sort"    "FMT") Func main () {    // Gets the index    of an integer from an ordered integer slice = []int{1,2,3,4,5}    res:= sort. Searchints (a,2)    FMT. Println (RES)    // get the index    of a floating-point number from an ordered floating-point slice = []float64{1.1,10.8,23.1,40.6,43.9}    Res2:= sort. searchfloat64s (b,43.9)    FMT. Println (res2)    // get the index    of a string from an ordered string slice = []string{"AA", "BB", "CC", "DD"}    Res3: = sort. Searchstrings (C, "CC")    FMT. Println (RES3)}
Special sort (also an application of the interface)

The above-mentioned methods have been provided in the sort package for the ordering of basic data types such as: int,float64,string, but for special data types such as: Map,struct and so on, we need to use sort for this sort. Sort method
Website address: https://golang.google.cn/pkg/sort/#Sort
Such as:

When we open the interface we can see the following:

From what we can see interface is actually an interface type, as long as we have the document said Len (), less (i,j int) bool,swap (i,j int) implemented interface this interface, the same we can call the sort. Sort method, using the following example to understand

Package Mainimport ("FMT" "Math/rand" "Sort") type Student struct{name string ageintscore Float32}type Studentslice []*Studentfunc (P studentslice) Len ()int{return Len (P)}func (P studentslice) less (i,jint) bool{return p[i].age>P[j].age}func (P studentslice) Swap (i,jint) {P[i],p[j]=P[j],p[i]}func Main () {var studentarr studentslice for I:=0;i<10;i++{var s= &student{name:fmt. Sprintf ("Zhao%d", i), Age:rand. INTN (100), Score:rand. Float32 ()*100,} Studentarr=append (studentarr,s)} fmt. Println ("Before sorting:") for I:=0;i<len (Studentarr); i++{fmt. Println (Studentarr[i])} sort. Sort (Studentarr) fmt. Println ("After sorting:") for I:=0;i<len (Studentarr); i++{fmt. Println (Studentarr[i])}}

Go basics-Sort and find operations

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.