Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. import (
  2. "fmt"
  3. "sort"
  4. )
  5.  
  6. type Sequence []int
  7.  
  8. func (s Sequence) Len() int {
  9. return len(s)
  10. }
  11.  
  12. func (s Sequence) Less(i, j int) bool {
  13. return s[i] < s[j]
  14. }
  15.  
  16. func (s Sequence) Swap(i, j int) {
  17. s[i], s[j] = s[j], s[i]
  18. }
  19.  
  20. func (s Sequence) Copy() Sequence {
  21. copy := make(Sequence, 0, len(s))
  22. return append(copy, s...)
  23. }
  24.  
  25. func main() {
  26. s := Sequence{3, 4, 5, 1, 2, 0}
  27. t := s.Copy()
  28. fmt.Println(t)
  29. sort.Sort(t) // Sort becomes available for type Sequence because we
  30. // implemented the required Len, Less, Swap methods.
  31. fmt.Println(fmt.Sprint([]int(t)))
  32.  
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement