To sort an array in ascending order in Go, you can use the `sort` package's `Ints` and `Strings` functions.
In this example,
numbers
.sort.Ints
function to sort the array in-place.package main
import (
"fmt"
"sort"
)
func main() {
numbers := []int{5, 2, 8, 1, 9}
sort.Ints(numbers)
fmt.Println("Sorted Array of Integers:", numbers)
}
Sorted Array of Integers: [1 2 5 8 9]
In this example,
words
.sort.Strings
function to sort the array in-place based on string comparison.package main
import (
"fmt"
"sort"
)
func main() {
words := []string{"apple", "banana", "cherry", "date"}
sort.Strings(words)
fmt.Println("Sorted Array of Strings:", words)
}
Sorted Array of Strings: [apple banana cherry date]
In this tutorial, we learned How to Sort an Array in Ascending Order in Go language with well detailed examples.