Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import (
- "fmt"
- "sort"
- )
- type Sequence []int
- func (s Sequence) Len() int {
- return len(s)
- }
- func (s Sequence) Less(i, j int) bool {
- return s[i] < s[j]
- }
- func (s Sequence) Swap(i, j int) {
- s[i], s[j] = s[j], s[i]
- }
- func (s Sequence) Copy() Sequence {
- copy := make(Sequence, 0, len(s))
- return append(copy, s...)
- }
- func main() {
- s := Sequence{3, 4, 5, 1, 2, 0}
- t := s.Copy()
- fmt.Println(t)
- sort.Sort(t) // Sort becomes available for type Sequence because we
- // implemented the required Len, Less, Swap methods.
- fmt.Println(fmt.Sprint([]int(t)))
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement