golang sort slice ascending or descending
You're looking for sort.Reverse
. That will let you say:
sort.Sort(sort.Reverse(fooAscending(s)))
As of Go 1.8, there is an easier way to sort a slice that does not require you to define new types. You simply pass an anonymous function to the sort.Slice
function.
a := []int{5, 3, 4, 7, 8, 9}
sort.Slice(a, func(i, j int) bool {
return a[i] < a[j]
})
for _, v := range a {
fmt.Println(v)
}
This will sort in ascending order, if you want the opposite, simply write a[i] > a[j]
in the anonymous function.