refactor: sort

This commit is contained in:
Inanc Gumus
2019-10-21 12:45:12 +03:00
parent 47b24c7649
commit 0fd5c2968a
5 changed files with 38 additions and 17 deletions

View File

@ -21,8 +21,6 @@ func (l list) String() string {
return "Sorry. We're waiting for delivery 🚚.\n" return "Sorry. We're waiting for delivery 🚚.\n"
} }
sort.Sort(l)
var str strings.Builder var str strings.Builder
for _, p := range l { for _, p := range l {
// fmt.Printf("* %s\n", p) // fmt.Printf("* %s\n", p)
@ -39,10 +37,12 @@ func (l list) discount(ratio float64) {
} }
} }
// implementation of the sort.Interface:
// by default `list` sorts by `title`. // by default `list` sorts by `title`.
func (l list) Len() int { return len(l) } func (l list) Len() int { return len(l) }
func (l list) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
func (l list) Less(i, j int) bool { return l[i].title < l[j].title } func (l list) Less(i, j int) bool { return l[i].title < l[j].title }
func (l list) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
// byRelease sorts by product release dates. // byRelease sorts by product release dates.
type byRelease struct { type byRelease struct {
@ -50,8 +50,20 @@ type byRelease struct {
list list
} }
// Less takes priority over the Less method of the `list`.
// `sort.Sort` will first call this method instead of the `list`'s Less method.
func (bp byRelease) Less(i, j int) bool { func (bp byRelease) Less(i, j int) bool {
// Before() accepts a time.Time but `released` is not time.Time. // `Before()` accepts a `time.Time` but `released` is not `time.Time`.
// `released.Time` is necessary.
return bp.list[i].released.Before(bp.list[j].released.Time) return bp.list[i].released.Before(bp.list[j].released.Time)
} }
/*
Anonymous embedding means auto-forwarding method calls to the embedded value:
func (bp byRelease) Len() int { return bp.list.Len() }
func (bp byRelease) Swap(i, j int) { bp.list.Swap(i, j) }
*/
func byReleaseDate(l list) sort.Interface {
return &byRelease{l}
}

View File

@ -9,7 +9,6 @@ package main
import ( import (
"fmt" "fmt"
"sort"
) )
func main() { func main() {
@ -21,8 +20,27 @@ func main() {
// sort.Sort(l) // sort.Sort(l)
// sort.Sort(sort.Reverse(l)) // sort.Sort(sort.Reverse(l))
// sort.Sort(byRelease{l}) // sort.Sort(byReleaseDate(l))
sort.Sort(sort.Reverse(byRelease{l})) // sort.Sort(sort.Reverse(byReleaseDate(l)))
fmt.Print(l) fmt.Print(l)
} }
/*
Summary:
- sort.Sort() can sort any type that implements the sort.Interface
- sort.Interface has three methods: Len(), Less(), Swap()
- Len() returns the length of a collection.
- Less() should return true when an element comes before another one.
- Swap()s the elements when Less() returns true.
- sort.Reverse() sorts a sort.Interface value.
- You can customize the sorting:
- by anonymously embedding the sort.Interface type
- and adding a Less() method.
- Anonymous embedding means auto-forwarding method calls to an embedded value.
*/

View File

@ -8,7 +8,6 @@
package main package main
import ( import (
"sort"
"strings" "strings"
) )
@ -19,8 +18,6 @@ func (l list) String() string {
return "Sorry. We're waiting for delivery 🚚.\n" return "Sorry. We're waiting for delivery 🚚.\n"
} }
sort.Sort(l)
var str strings.Builder var str strings.Builder
for _, p := range l { for _, p := range l {
// fmt.Printf("* %s\n", p) // fmt.Printf("* %s\n", p)

View File

@ -8,7 +8,6 @@
package main package main
import ( import (
"sort"
"strings" "strings"
) )
@ -19,8 +18,6 @@ func (l list) String() string {
return "Sorry. We're waiting for delivery 🚚.\n" return "Sorry. We're waiting for delivery 🚚.\n"
} }
sort.Sort(l)
var str strings.Builder var str strings.Builder
for _, p := range l { for _, p := range l {
// fmt.Printf("* %s\n", p) // fmt.Printf("* %s\n", p)

View File

@ -8,7 +8,6 @@
package main package main
import ( import (
"sort"
"strings" "strings"
) )
@ -19,8 +18,6 @@ func (l list) String() string {
return "Sorry. We're waiting for delivery 🚚.\n" return "Sorry. We're waiting for delivery 🚚.\n"
} }
sort.Sort(l)
var str strings.Builder var str strings.Builder
for _, p := range l { for _, p := range l {
// fmt.Printf("* %s\n", p) // fmt.Printf("* %s\n", p)