# 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.