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

@ -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.
*/