sort items in array by desc in Go
Code snippet on how to sort items in array by desc in Go
package main
import (
"fmt"
"sort"
)
func main() {
arr := []int{5, 8, 7, 2, 4, 6, 1, 3}
sort.Sort(sort.Reverse(sort.IntSlice(arr)))
fmt.Println(arr)
}
The code starts by importing the fmt
and sort
packages. Then, creates a variable arr
that stores an array of 8 integers. The code uses the sort.Sort()
method to reverse sort the arr
array and store the result back in arr
. Lastly, the fmt.Println()
method is used to print the sorted array.