Mastering-Go-Second-Edition/ch03/sortSlice.go
Mihalis Tsoukalos ba2d8712e7 Minor correction
2019-01-24 20:37:30 +02:00

31 lines
725 B
Go

package main
import (
"fmt"
"sort"
)
type aStructure struct {
person string
height int
weight int
}
func main() {
mySlice := make([]aStructure, 0)
mySlice = append(mySlice, aStructure{"Mihalis", 180, 90})
mySlice = append(mySlice, aStructure{"Bill", 134, 45})
mySlice = append(mySlice, aStructure{"Marietta", 155, 45})
mySlice = append(mySlice, aStructure{"Epifanios", 144, 50})
mySlice = append(mySlice, aStructure{"Athina", 134, 40})
fmt.Println("0:", mySlice)
sort.Slice(mySlice, func(i, j int) bool {
return mySlice[i].height < mySlice[j].height
})
fmt.Println("<:", mySlice)
sort.Slice(mySlice, func(i, j int) bool {
return mySlice[i].height > mySlice[j].height
})
fmt.Println(">:", mySlice)
}