Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
41 lines (31 sloc) 1.06 KB
/*
Title: 39.Sorting.go
Author: OpenSource
Date: 2017-05-22
Description: For Study
Go’s sort package implements sorting for builtins and user-defined types.
We’ll look at sorting for builtins first.
*/
package main
import (
"sort"
"fmt"
)
func main(){
fmt.Println("39.Sorting.go---------Start------------\n\n")
// Sort methods are specific to the builtin type; here’s an example for strings.
// Note that sorting is in-place, so it changes the given slice and doesn’t return a new one.
strs := []string{"c", "a", "b"}
sort.Strings(strs)
fmt.Println("Strings =>", strs)
// An example of sorting ints.
ints := []int{7, 2, 4}
sort.Ints(ints)
fmt.Println("Ints =>",ints)
// We can also use sort to check if a slice is already in sorted order.
s := sort.IntsAreSorted(ints)
fmt.Println("sort.IntsAreSorted(ints) => ", s)
fmt.Println("39.Sorting.go---------Start------------\n\n")
// Running our program prints the sorted string and int slices and true
// as the result of our AreSorted test.
}
You can’t perform that action at this time.