2019-10-17 14:11:51 +03:00
|
|
|
|
// Copyright © 2018 Inanc Gumus
|
|
|
|
|
// Learn Go Programming Course
|
|
|
|
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
|
|
|
//
|
2019-10-30 19:34:44 +03:00
|
|
|
|
// For more tutorials : https://learngoprogramming.com
|
|
|
|
|
// In-person training : https://www.linkedin.com/in/inancgumus/
|
|
|
|
|
// Follow me on twitter: https://twitter.com/inancgumus
|
2019-10-17 14:11:51 +03:00
|
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2019-10-23 21:00:05 +03:00
|
|
|
|
"sort"
|
2019-10-17 14:11:51 +03:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
l := list{
|
|
|
|
|
{title: "moby dick", price: 10, released: toTimestamp(118281600)},
|
|
|
|
|
{title: "odyssey", price: 15, released: toTimestamp("733622400")},
|
|
|
|
|
{title: "hobbit", price: 25},
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-09 20:40:03 +03:00
|
|
|
|
// sort.Sort(l)
|
|
|
|
|
// sort.Sort(sort.Reverse(l))
|
|
|
|
|
// sort.Sort(byReleaseDate(l))
|
2019-10-23 21:00:05 +03:00
|
|
|
|
sort.Sort(sort.Reverse(byReleaseDate(l)))
|
2019-10-17 14:11:51 +03:00
|
|
|
|
|
|
|
|
|
fmt.Print(l)
|
|
|
|
|
}
|
2019-10-21 12:45:12 +03:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
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.
|
2019-10-23 21:00:05 +03:00
|
|
|
|
- Less(i, j) should return true when an element comes before another one.
|
|
|
|
|
- Swap(i, j)s the elements when Less() returns true.
|
2019-10-21 12:45:12 +03:00
|
|
|
|
|
2019-10-23 21:00:05 +03:00
|
|
|
|
- sort.Reverse() can reverse sort a type that satisfies the sort.Interface.
|
2019-10-21 12:45:12 +03:00
|
|
|
|
|
|
|
|
|
- You can customize the sorting:
|
2019-10-23 21:00:05 +03:00
|
|
|
|
- Either by implementing the sort.Interface methods,
|
|
|
|
|
- or by anonymously embedding a type that already satisfies the sort.Interface
|
2019-10-21 12:45:12 +03:00
|
|
|
|
- and adding a Less() method.
|
|
|
|
|
|
|
|
|
|
- Anonymous embedding means auto-forwarding method calls to an embedded value.
|
|
|
|
|
*/
|