Skip to content

Instantly share code, notes, and snippets.

@santiaago
Last active May 16, 2025 16:06
Show Gist options
  • Select an option

  • Save santiaago/9027077 to your computer and use it in GitHub Desktop.

Select an option

Save santiaago/9027077 to your computer and use it in GitHub Desktop.

Revisions

  1. santiaago renamed this gist Feb 15, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. santiaago created this gist Feb 15, 2014.
    29 changes: 29 additions & 0 deletions sortByDate
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    package main

    import "fmt"
    import "time"
    import "sort"

    func main() {
    fmt.Println("Sorting an array of time")
    const shortForm = "Jan/02/2006"
    t1, _ := time.Parse(shortForm, "Feb/02/2014")
    t2, _ := time.Parse(shortForm, "Feb/02/1800")
    t3, _ := time.Parse(shortForm, "Feb/02/1999")
    t4, _ := time.Parse(shortForm, "Feb/02/2000")
    dates := []time.Time{t1, t2, t3, t4}
    for _, t := range dates {
    fmt.Println(t)
    }
    sort.Sort(ByDate(dates))
    fmt.Println("sorted:")
    for _, t := range dates {
    fmt.Println(t)
    }
    }

    type ByDate []time.Time

    func (a ByDate) Len() int { return len(a) }
    func (a ByDate) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
    func (a ByDate) Less(i, j int) bool { return a[i].Before(a[j]) }