diff --git a/interfaces/11-sort/list.go b/interfaces/11-sort/list.go index 69a9a85..722b337 100644 --- a/interfaces/11-sort/list.go +++ b/interfaces/11-sort/list.go @@ -21,8 +21,6 @@ func (l list) String() string { return "Sorry. We're waiting for delivery 🚚.\n" } - sort.Sort(l) - var str strings.Builder for _, p := range l { // 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`. 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) Swap(i, j int) { l[i], l[j] = l[j], l[i] } // byRelease sorts by product release dates. type byRelease struct { @@ -50,8 +50,20 @@ type byRelease struct { 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 { - // Before() accepts a time.Time but `released` is not time.Time. - // `released.Time` is necessary. + // `Before()` accepts a `time.Time` but `released` is not `time.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} +} diff --git a/interfaces/11-sort/main.go b/interfaces/11-sort/main.go index 3691fed..1ebb40d 100644 --- a/interfaces/11-sort/main.go +++ b/interfaces/11-sort/main.go @@ -9,7 +9,6 @@ package main import ( "fmt" - "sort" ) func main() { @@ -21,8 +20,27 @@ func main() { // sort.Sort(l) // sort.Sort(sort.Reverse(l)) - // sort.Sort(byRelease{l}) - sort.Sort(sort.Reverse(byRelease{l})) + // sort.Sort(byReleaseDate(l)) + // sort.Sort(sort.Reverse(byReleaseDate(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. +*/ diff --git a/interfaces/12-marshaler/list.go b/interfaces/12-marshaler/list.go index 018b629..b501e9f 100644 --- a/interfaces/12-marshaler/list.go +++ b/interfaces/12-marshaler/list.go @@ -8,7 +8,6 @@ package main import ( - "sort" "strings" ) @@ -19,8 +18,6 @@ func (l list) String() string { return "Sorry. We're waiting for delivery 🚚.\n" } - sort.Sort(l) - var str strings.Builder for _, p := range l { // fmt.Printf("* %s\n", p) diff --git a/interfaces/13-io/list.go b/interfaces/13-io/list.go index 018b629..b501e9f 100644 --- a/interfaces/13-io/list.go +++ b/interfaces/13-io/list.go @@ -8,7 +8,6 @@ package main import ( - "sort" "strings" ) @@ -19,8 +18,6 @@ func (l list) String() string { return "Sorry. We're waiting for delivery 🚚.\n" } - sort.Sort(l) - var str strings.Builder for _, p := range l { // fmt.Printf("* %s\n", p) diff --git a/interfaces/14-composition/list.go b/interfaces/14-composition/list.go index 018b629..b501e9f 100644 --- a/interfaces/14-composition/list.go +++ b/interfaces/14-composition/list.go @@ -8,7 +8,6 @@ package main import ( - "sort" "strings" ) @@ -19,8 +18,6 @@ func (l list) String() string { return "Sorry. We're waiting for delivery 🚚.\n" } - sort.Sort(l) - var str strings.Builder for _, p := range l { // fmt.Printf("* %s\n", p)