The 'sort.Strings()' Function is used to sort a String Array in Ascending order.
package main import "fmt" import "sort" func main() { var arr = []string{"Mohan", "John", "Paul", "Kriti", "Salim"} sort.Strings(arr) fmt.Println("The Sorted Array in ascending order is ",arr) }
So, in the above code we have created a 'Array' and initialised to the variable 'x'.
Below is how the values are positioned in the Array,
Now, to sort the String Array, Go provides us with a 'sort' package. So, to use the 'sort' package, we need to import it first.
Then we have used the 'Strings()' Function from the 'sort' package to sort the Array 'arr' in ascending order.
And the Array 'arr' gets sorted with 'John' as the first value, 'Kriti' second and 'Mohan' as the third, 'Paul' as fourth and 'Salim' as the fifth value.
And we get the below output.
Even here the 'sort' package to sort an integer Array with numbers in Increasing Order.
package main import "fmt" import "sort" func main() { var arr = []int{5, 3, 2, 4} sort.Ints(arr) fmt.Println("The Sorted Array in ascending order is ",arr) }
So, in the above code we have created an Integer 'Array' and initialised to the variable 'arr'.
Below is how the values are positioned in the Array,
Then we have used the 'Ints()' Function from the 'sort' package to sort the Array 'x' in increasing order.
And the numbers in the Array 'x' gets sorted.
And we get the below output.