This commit is contained in:
Mark McGranaghan 2012-09-21 09:03:42 -07:00
parent 5a2e750fe1
commit 5520ee6b0b

View File

@ -1,20 +1,28 @@
package main
import "fmt"
package main // The `sort` package implements sorting for builtins
// and user-defined types. We'll look at some of the
import "fmt" // sorts for builtins here.
import "sort"
func main() {
strs := []string{"c", "a", "b"}
sort.Strings(strs)
fmt.Println(strs)
sort.Strings(strs) // Sort methods are specific to the builtin type.
fmt.Println(strs) // Sorting is in-place (doesn't return a new slice).
ints := []int{7, 2, 4}
sort.Ints(ints)
sort.Ints(ints) // Sorting methods are named after the type.
fmt.Println(ints)
s := sort.IntsAreSorted(ints) // Check if a slice is in sorted order.
fmt.Println(s)
i := sort.SearchInts(ints, 4) // Binary-search a sorted slice for a value.
fmt.Println(i) // Returns the index if found, or `-1` if not.
}
/*
$ go run sort.go
[foo bar bat]
[a b c]
[2 4 7]
true
1
*/