From 5520ee6b0b249b7816feda2222f60e8eaa94e05a Mon Sep 17 00:00:00 2001 From: Mark McGranaghan Date: Fri, 21 Sep 2012 09:03:42 -0700 Subject: [PATCH] sort --- 084-sort.go | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/084-sort.go b/084-sort.go index 2bcc24f..a3fcd77 100644 --- a/084-sort.go +++ b/084-sort.go @@ -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 */