go language from the beginning of the example Example38. Sort

Go to  sort package implements a built-in and user-defined data type sorting. We focus first built-in data type of the sort.

Example:

main Package
 Import (
     " FMT " 
    " Sort " 
) 


FUNC main () {
     // sorted positive for the built-in data types; Here is an example of a string.
    // Note that ordering is updated in place, so he will change in a given sequence and does not return a new value 
    strarr: = [] {String " F " , " D " , " A " , " H " } 
    sort.Strings (strarr) 
    fmt.Println ( " Strings: " , strarr)

     // int type sort 
    intArr: = [] {int. 6,. 7,. 1,. 3 }
    sort.Ints (intArr)
    fmt.Println ( " Ints: " , intArr)

     // check whether the array is already sorted, s sorted true, otherwise false 
    S: = sort.IntsAreSorted (intArr) 
    fmt.Println ( " the Sorted: " , s)

      a := sort.StringsAreSorted(strarr)
      fmt.Println("Sorted:", a)

}

Result:

$ go run example.go
Strings: [a d f h]
Ints: [1 3 6 7]
Sorted: true

Sorted: true

 

Coordinates: previous example    next example

 

Guess you like

Origin www.cnblogs.com/yhleng/p/11772379.html