remove number for source
This commit is contained in:
54
src/sorting-by-functions/sorting-by-functions.go
Normal file
54
src/sorting-by-functions/sorting-by-functions.go
Normal file
@@ -0,0 +1,54 @@
|
||||
// ## Sorting by Functions
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
)
|
||||
|
||||
type Person struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
|
||||
type ByName []Person
|
||||
|
||||
func (this ByName) Len() int {
|
||||
return len(this)
|
||||
}
|
||||
func (this ByName) Less(i, j int) bool {
|
||||
return this[i].Name < this[j].Name
|
||||
}
|
||||
func (this ByName) Swap(i, j int) {
|
||||
this[i], this[j] = this[j], this[i]
|
||||
}
|
||||
|
||||
type ByAge []Person
|
||||
|
||||
func (this ByAge) Len() int {
|
||||
return len(this)
|
||||
}
|
||||
func (this ByAge) Less(i, j int) bool {
|
||||
return this[i].Age < this[j].Age
|
||||
}
|
||||
func (this ByAge) Swap(i, j int) {
|
||||
this[i], this[j] = this[j], this[i]
|
||||
}
|
||||
|
||||
func main() {
|
||||
kids := []Person{
|
||||
{"Jack", 10},
|
||||
{"Jill", 9},
|
||||
{"Bob", 12},
|
||||
}
|
||||
fmt.Println("Original:", kids)
|
||||
|
||||
sort.Sort(ByName(kids))
|
||||
fmt.Println("ByName: ", kids)
|
||||
|
||||
sort.Sort(ByAge(kids))
|
||||
fmt.Println("ByAge: ", kids)
|
||||
}
|
||||
|
||||
// todo: pull from blog post
|
||||
4
src/sorting-by-functions/sorting-by-functions.sh
Normal file
4
src/sorting-by-functions/sorting-by-functions.sh
Normal file
@@ -0,0 +1,4 @@
|
||||
$ go run sorting-by-function.go
|
||||
Original: [{Jack 10} {Jill 9} {Bob 12}]
|
||||
ByName: [{Bob 12} {Jack 10} {Jill 9}]
|
||||
ByAge: [{Jill 9} {Jack 10} {Bob 12}]
|
||||
Reference in New Issue
Block a user