
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Sort Array Elements in Ascending Order using Golang
In this tutorial, we will see to write a go language program to sort an array in ascending order.
Sort An Array In Ascending Order Using A User-Defined Function
The following code illustrates how we can sort an array of elements in ascending order in golang.
Algorithm
Step 1 ? Import the fmt package.
Step 2 ? Define a function sortArray() to sort the given array.
Step 3 ? Pass arguments to sortArray() function one is the array of integers that we wish to sort and the other two variables are used to hold temporary values.
Step 4 ? Use for loops and if condition to sort the array. The first for loop is used to iterate over the unsorted array.
Step 5 ? The second for loop is used to get the min value present in the array. Then by using a temporary variable, we are putting the smaller value after the larger one.
Step 6 ? Start the main() function.
Step 7 ? Initialize an array of integers and store values in it. print the unsorted array on the screen.
Step 8 ? Then we need to call the sortArray() function by passing the array to be sorted along with temp and min integer type variables.
Step 9 ? Store the array returned by the function and print it on the screen using fmt.Println() function.
Example
package main import "fmt" func sortArray(arr [5]int, min int, temp int) [5]int { for i := 0; i <= 4; i++ { min = i for j := i + 1; j <= 4; j++ { if arr[j] < arr[min] { // changing the index to show the min value min = j } } temp = arr[i] arr[i] = arr[min] arr[min] = temp } return arr } func main() { arr := [5]int{50, 30, 20, 10, 40} fmt.Println("The unsorted array entered is:", arr) var min int = 0 var temp int = 0 array := sortArray(arr, min, temp) fmt.Println() fmt.Println("The final array obtained after sorting is:", array) }
Output
The unsorted array entered is: [50 30 20 10 40] The final array obtained after sorting is: [10 20 30 40 50]
Sort An Array Of Integers Using Sort Functions
The following code illustrates to sort an array of strings in Go programming language using predefined functions.
Syntax
sort.Ints(arr)
The Ints function is present in the sort package and it takes the integer array to be sorted as an argument to the function. It then returns the array by sorting it in ascending order.
Algorithm
Step 1 ? Import the fmt and sort packages.
Step 2 ? Start the main() function.
Step 3 ? Initialize an array of integers and store values in it. Print the unsorted array on the screen.
Step 4 ? Now we need to call the function Ints() present in the sort package by passing the array to be sorted as an argument to the function.
Step 5 ? The arr array is sorted. Print it on the screen using fmt.Println() function.
Example
package main import ( "fmt" "sort" ) func main() { var arr = []int{9, 8, 7, 4, 5, 3} fmt.Println("Unsorted array of strings is", arr) sort.Ints(arr) fmt.Println("The above array is sorted and the result is:", arr) }
Output
Unsorted array of strings is [9 8 7 4 5 3] The above array is sorted and the result is: [3 4 5 7 8 9]
Sort An Array Of Strings In Ascending Order Using The Slicing Method
Let us now write a program to sort an array of strings in ascending order using predefined functions in Go programming language.
Syntax
sort.Sort(sort.StringSlice(arr))
The StringSlice() function is present in the sort package and it takes the array of strings to be sorted as an argument and returns the sorted string.
Algorithm
Step 1 ? Import the fmt and sort packages.
Step 2 ? Start the main() function.
Step 3 ? Initialize an array of strings and store values to it. Print the unsorted array on the screen.
Step 4 ? Now we need to call the function StringSlice() present in the sort package by passing the array to be sorted as an argument to the function.
Step 5 ? The arr array is now sorted. We can print it on the screen using fmt.Println() function.
Example
package main import ( "fmt" "sort" ) func main() { var arr = []string{"s", "d", "c", "b", "a"} fmt.Println("Unsorted array of strings is", arr) sort.Sort(sort.StringSlice(arr)) fmt.Println("The above array is sorted and the result is:", arr) }
Output
Unsorted array of strings is [s d c b a] The above array is sorted and the result is: [a b c d s]
Conclusion
We have successfully compiled and executed a go language program to sort an array in ascending order along with examples. In the first example, we used a user-defined function, in the second example, we used an inbuild function of Sort() and in the third example, we used another method of Slicing.