sorting
This commit is contained in:
parent
5bb6b01f89
commit
f777d74cc5
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user