sorting
This commit is contained in:
parent
5bb6b01f89
commit
f777d74cc5
@ -1,11 +1,13 @@
|
|||||||
// ## Sorting by Functions
|
// ## 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
|
package main
|
||||||
|
|
||||||
import (
|
import "fmt"
|
||||||
"fmt"
|
import "sort"
|
||||||
"sort"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Person struct {
|
type Person struct {
|
||||||
Name string
|
Name string
|
||||||
@ -50,5 +52,3 @@ func main() {
|
|||||||
sort.Sort(ByAge(kids))
|
sort.Sort(ByAge(kids))
|
||||||
fmt.Println("ByAge: ", kids)
|
fmt.Println("ByAge: ", kids)
|
||||||
}
|
}
|
||||||
|
|
||||||
// todo: pull from blog post
|
|
||||||
|
@ -1,28 +1,31 @@
|
|||||||
// ## Sorting
|
// ## Sorting
|
||||||
|
|
||||||
// The `sort` package implements sorting for builtins
|
// Go's `sort` package implements sorting for builtins
|
||||||
// and user-defined types. We'll look at some of the
|
// and user-defined types. We'll look at sorting for
|
||||||
// sorts for builtins here.
|
// builtins first.
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "fmt"
|
import "fmt"
|
||||||
import "sort"
|
import "sort"
|
||||||
|
|
||||||
func main() {
|
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"}
|
strs := []string{"c", "a", "b"}
|
||||||
sort.Strings(strs)
|
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}
|
ints := []int{7, 2, 4}
|
||||||
sort.Ints(ints)
|
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)
|
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
|
$ go run sorting.go
|
||||||
[a b c]
|
Strings: [a b c]
|
||||||
[2 4 7]
|
Ints: [2 4 7]
|
||||||
true
|
Sorted: true
|
||||||
1
|
|
||||||
|
@ -32,7 +32,7 @@ func main() {
|
|||||||
lines := readLines(sourcePath)
|
lines := readLines(sourcePath)
|
||||||
for _, line := range lines {
|
for _, line := range lines {
|
||||||
if !foundLongLine && !todoPat.MatchString(line) && (len(line) > 58) {
|
if !foundLongLine && !todoPat.MatchString(line) && (len(line) > 58) {
|
||||||
fmt.Println(sourcePath)
|
fmt.Println("measure:", sourcePath)
|
||||||
foundLongLine = true
|
foundLongLine = true
|
||||||
foundLongFile = true
|
foundLongFile = true
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user