diff --git a/src/sorting-by-functions/sorting-by-functions.go b/src/sorting-by-functions/sorting-by-functions.go index ebafaf3..c68e30d 100644 --- a/src/sorting-by-functions/sorting-by-functions.go +++ b/src/sorting-by-functions/sorting-by-functions.go @@ -1,11 +1,13 @@ // ## Sorting by Functions +// Sorting a slice by a function is a bit tricker in Go +// than you may be used to in other languages. Let's look +// at some examples to see how it works. + package main -import ( - "fmt" - "sort" -) +import "fmt" +import "sort" type Person struct { Name string @@ -50,5 +52,3 @@ func main() { sort.Sort(ByAge(kids)) fmt.Println("ByAge: ", kids) } - -// todo: pull from blog post diff --git a/src/sorting/sorting.go b/src/sorting/sorting.go index b3b766c..3b506be 100644 --- a/src/sorting/sorting.go +++ b/src/sorting/sorting.go @@ -1,28 +1,31 @@ // ## Sorting -// The `sort` package implements sorting for builtins -// and user-defined types. We'll look at some of the -// sorts for builtins here. +// Go's `sort` package implements sorting for builtins +// and user-defined types. We'll look at sorting for +// builtins first. + package main import "fmt" import "sort" func main() { - // Sort methods are specific to the builtin type. - // Sorting is in-place (doesn't return a new slice). + + // 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(strs) + fmt.Println("Strings:", strs) - // Sorting methods are named after the type. + // An example of sorting `int`s. ints := []int{7, 2, 4} sort.Ints(ints) - fmt.Println(ints) + fmt.Println("Ints: ", ints) - // Check if a slice is in sorted order. + // We can also use `sort` to check if a slice is + // already in sorted order. s := sort.IntsAreSorted(ints) - fmt.Println(s) + fmt.Println("Sorted: ", s) } - -// todo: general and convenience searching diff --git a/src/sorting/sorting.sh b/src/sorting/sorting.sh index bd7ca06..c0e7462 100644 --- a/src/sorting/sorting.sh +++ b/src/sorting/sorting.sh @@ -1,5 +1,6 @@ +# Running our program prints the sorted string and int +# slices and `true` as the result of our `AreSorted` test. $ go run sorting.go -[a b c] -[2 4 7] -true -1 +Strings: [a b c] +Ints: [2 4 7] +Sorted: true diff --git a/tool/measure.go b/tool/measure.go index 766f080..3fd0b23 100644 --- a/tool/measure.go +++ b/tool/measure.go @@ -32,7 +32,7 @@ func main() { lines := readLines(sourcePath) for _, line := range lines { if !foundLongLine && !todoPat.MatchString(line) && (len(line) > 58) { - fmt.Println(sourcePath) + fmt.Println("measure:", sourcePath) foundLongLine = true foundLongFile = true }