Sorting slice in Go is straightforward. You just need to pass a “less” function that tells whether the first argument index should be placed after the second argument index. In our example, the dates are in ascending order. Using time’s After function, we return “false” if it’s in ascending order, which in turn the sort function will do the opposite, which is to order it descendingly.
ts := []time.Time{time.Now(), time.Now().AddDate(0, 0, 1)} // [2009-11-10 23:00:00 +0000 UTC m=+0.000000001 2009-11-11 23:00:00 +0000 UTC]
fmt.Println(ts)
sort.Slice(ts, func(i, j int) bool {
return ts[i].After(ts[j])
})
fmt.Println(ts) // [2009-11-11 23:00:00 +0000 UTC 2009-11-10 23:00:00 +0000 UTC m=+0.000000001]