fork(1) download
  1. package main
  2.  
  3. import "fmt"
  4. import "time"
  5. import "sort"
  6.  
  7. // Define us a type so we can sort it
  8. type TimeSlice []time.Time
  9.  
  10. func main() {
  11. events := make([]time.Time, 2) // Create slice of two times
  12. events[0], _ = time.Parse("15:04", "04:30") // 4:30AM
  13. events[1], _ = time.Parse("15:04", "15:12") // 3:12PM
  14. sort.Sort(TimeSlice(events)) // Wrap array in type for sorting
  15.  
  16. fmt.Println(events)
  17. }
  18.  
  19. // Forward request for length
  20. func (p TimeSlice) Len() int {
  21. return len(p) }
  22.  
  23. // Define compare
  24. func (p TimeSlice) Less(i, j int) bool {
  25. return p[i].Before(p[j]) }
  26.  
  27. // Define swap over an array
  28. func (p TimeSlice) Swap(i, j int) {
  29. p[i], p[j] = p[j], p[i] }
Success #stdin #stdout 0s 420672KB
stdin
Standard input is empty
stdout
[0000-01-01 04:30:00 +0000 UTC 0000-01-01 15:12:00 +0000 UTC]