# How to sort array of ints in Golang [SOLVED]

Written By - Tuan Nguyen

In the previous post, we discussed sorting slices in golang. In this tutorial, we will walk through how to sort an array of ints in Golang.

`array`: In computer science, an array is a data structure consisting of a collection of elements (values or variables), each identified by at least one array index or key. An array is stored such that the position of each element can be computed from its index tuple by a mathematical formula. The simplest type of data structure is a linear array, also called a one-dimensional array.

## Example 1: Convert to int slice and then use the Ints() function

`func Ints(x []int)`: Ints sorts a slice of ints in increasing order.

The below example demonstrates a simple way to sort an int array with `Ints()` function:

``````package main

import (
"fmt"
"reflect"
"sort"
)

func main() {
var arr int
arr = 5
arr = 6
arr = 2
arr = 7
arr = 1

fmt.Println("Before sort: ")
fmt.Println(arr)
fmt.Println("array = ", reflect.TypeOf(arr)) // check type of variable

sort.Ints(arr[:]) // convert array to slice and sort the array
fmt.Println("After sort: ")
fmt.Println(arr)
fmt.Println("array = ", reflect.TypeOf(arr))
}
``````

Output:

```Before sort:
[5 6 2 7 1]
array =  int
After sort:
[1 2 5 6 7]
array =  int```

## Example 2: Using Slice() function to sort int array in ascending order

In example 2, we will use `Slice()` function to sort an int array in ascending order:

`func Slice(x any, less func(i, j int) bool)`: Slice sorts the slice x given the provided less function. It panics if x is not a slice. The sort is not guaranteed to be stable: equal elements may be reversed from their original order. For a stable sort, use SliceStable. The less function must satisfy the same requirements as the Interface type's Less method.

``````package main

import (
"fmt"
"reflect"
"sort"
)

func main() {
var arr int
arr = 5
arr = 6
arr = 2
arr = 7
arr = 1

fmt.Println("Before sort: ")
fmt.Println(arr)
fmt.Println("array = ", reflect.TypeOf(arr)) // check type of variable

sort.Slice(arr[:], func(i, j int) bool {
return arr[:][i] > arr[:][j]
})
fmt.Println("After sort: ")
fmt.Println(arr)
fmt.Println("array = ", reflect.TypeOf(arr))
}``````

Output:

```Before sort:
[5 6 2 7 1]
array =  int
After sort:
[7 6 5 2 1]
array =  int```

## Example 3: Write function to do Bubble Sort an array

Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the input list element by element, comparing the current element with the one after it, swapping their values if needed. These passes through the list are repeated until no swaps had to be performed during a pass, meaning that the list has become fully sorted. The algorithm, which is a comparison sort, is named for the way the larger elements "bubble" up to the top of the list.

• Step 1: Iterate the array from 0th index to n-1.
• Step 2: Iterate the array from the 0th index to n-1-i, where i is the index of the above loop.
• Step 3: Swap if the highest element is at the starting position of an array, else leave.
• Step 3: At the end, return the array.

Time Complexity: O(n2)

Here is an example of implementing bubble sort an array in Go:

``````package main

import "fmt"

func main() {
var arr int
arr = 5
arr = 6
arr = 2
arr = 7
arr = 1

fmt.Println("Before sort: ")
fmt.Println(arr)
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]
}
}
}
fmt.Println("After sort: ")
fmt.Println(arr)

}``````

Output:

```Before sort:
[5 6 2 7 1]
After sort:
[1 2 5 6 7]```

## Summary

In this article I have given examples of sorting elements in an array. We can use built-in function in `sort` package or write our own function. Hopefully with the idea introduced, you can freely develop different sort int array applications

## References

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation. For any other feedbacks or questions you can either use the comments section or contact me form.