- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang Program To Sort An Array
In this tutorial, we will see to write a go language program to sort an array using three different methods.
Sort An Array Of Integers Using User-Defined Functions
The following code illustrates how we can sort an array of elements in golang using user-defined functions.
Algorithm
Step 1 − Importing the fmt package.
Step 2 − Defining a function named sortArray() which will sort the given array.
Step 3 − Pass the array to be sorted as an argument to this function. This function uses two for loops to iterate over the array.
Step 4 − If the current element of the array is greater than the previous element then we need to switch their positions.
Step 5 − Repeat the process until the for loop iterates to all the array elements. Return the new array thus formed.
Step 6 − Start the main function.
Step 7 − Initialize an array of integers and print it on the screen.
Step 8 − Call the sortArray() function.
Step 9 − Store the array returned by the function in a variable called result and print it on the screen using fmt.Println() function.
Example
package main import "fmt" // defining a sortArray function to sort the given array func sortArray(arr [5]int) [5]int { for i := 0; i <= len(arr)-1; i++ { for j := 0; j < len(arr)-1-i; j++ { if arr[j] > arr[j+1] { arr[j], arr[j+1] = arr[j+1], arr[j] } } } return arr } func main() { arr := [5]int{50, 30, 20, 10, 40} fmt.Println("The unsorted array entered is:", arr) result := sortArray(arr) fmt.Println("The sorted array is:", result) fmt.Println() arr = [5]int{2, 8, 6, 3, 1} fmt.Println("The unsorted array entered is:", arr) result = sortArray(arr) fmt.Println("The sorted array is:", result) }
Output
The unsorted array entered is: [50 30 20 10 40] The sorted array is: [10 20 30 40 50] The unsorted array entered is: [2 8 6 3 1] The sorted array is: [1 2 3 6 8]
Sort An Array Of Strings In Ascending Order Using A Pre-Defined Function
The following code illustrates to sort an array of strings in the Go programming language
Syntax
Sort.Strings(strs)
The Strings() 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 strings present in the sort package by passing the array to be sorted as an argument to the function.
Step 5 − The strs array is now sorted. We can print it on the screen using fmt.Println() function.
Example
package main import ( "fmt" "sort" ) func main() { var strs = []string{"c", "a", "b"} fmt.Println("Unsorted array of strings is", strs) sort.Strings(strs) fmt.Println("The above array is sorted and the result is:", strs) }
Output
Unsorted array of strings is [c a b] The above array is sorted and the result is: [a b c]
Conclusion
We have successfully compiled and executed a go language program to sort an array along with the examples.
- Related Articles
- Write a Golang program to sort an array using Bubble Sort
- Golang Program to sort an array in descending order using insertion sort
- Golang Program To Sort An Array In Ascending Order Using Insertion Sort
- Golang Program To Sort The Elements Of An Array In Ascending Order
- Golang Program To Sort The Elements Of An Array In Descending Order
- Golang Program to Sort the 2D Array Across Columns
- Python Program to Sort an Array
- Golang program to print an array?
- C program to sort an array by using merge sort
- Golang Program To Push An Array Into Another Array
- Golang Program to Convert String to an Array
- Write a Golang program to sort a binary array in linear time
- C program to sort an array in an ascending order
- Golang Program To Append An Element Into An Array
- Golang program to sort a string
