How to sort an array in Golang?

We will use the inbuilt sort package to sort the array in the Go language instead of manually writing any sorting algorithms like merge sort or quick sort, etc. In this post, we will be looking at how we can sort the array of different types like int, float64, etc., in ascending and descending order.

What all we will be looking at in this post –

Sorting an int array in ascending order

We will be using the Ints() function of the sort package to sort the int array in ascending order.

package main

import (
	"fmt"
	"sort"
)

func main() {
	arr := [...]int{12, 331, 31, 451, 54, 52} // unsorted
	arrSlice := arr[:]                        // created a slice of the array
	sort.Ints(arrSlice)
	fmt.Println("sorted array in ascending order:", arr)
}

Output –

sorted array: [12 31 52 54 331 451]
Internal implementation of Ints() function
func Ints(x []int) { Sort(IntSlice(x)) }

What does the Ints() method do? It sorts a slice of ints in increasing order.

So, how is the array getting sorted?
  • First, we created a slice from the array.
  • Then we passed the slice into the Ints() method.
  • The Ints() method wrapped the slice in an IntSlice that defines the Less, Swap, and Len method, which the Sort() method further uses while sorting the array.

Sorting an int array in descending order

package main

import (
	"fmt"
	"sort"
)

func main() {
	arr := [...]int{12, 331, 31, 451, 54, 52} // unsorted
	arrSlice := arr[:]                        // created a slice of the array
	sort.Sort(sort.Reverse(sort.IntSlice(arrSlice)))
	fmt.Println("sorted array in descending order:", arr)
}

Output –

sorted array in descending order: [451 331 54 52 31 12]
And now, what is happening here?
  • Here, first, we will again be using the sort.IntSlice(arr) which will wrap the array into an IntSlice that defines the Len, less and swap methods.
  • Then, sort.Reverse() method replaces the less method defined by the IntSlice by implementing the exact opposite of what we had in IntSlice.
  • So, now when we use the sort.Sort() method, which further uses the changed less method implementation to sort the array; then, the array will be sorted in descending order.

Now, let’s sort the arrays containing float64 data type elements. All internal implementations will be the same with one difference: now, we will be wrapping the array in Float64Slice instead of the IntSlice.

Sorting a float array in ascending order

Here we will be using the float64 data type for the array and the corresponding Float64s function of the sort package.

package main

import (
	"fmt"
	"sort"
)

func main() {
	arr := [...]float64{2.31, 33.31, 13.23, 41.31, 21.31, 452.2} // unsorted
	arrSlice := arr[:]
	sort.Float64s(arrSlice) 
	fmt.Println("sorted array:", arr)
}

Output –

sorted array: [2.31 13.23 21.31 33.31 41.31 452.2]

Sorting a float array in descending order

package main

import (
	"fmt"
	"sort"
)

func main() {
	arr := [...]float64{2.31, 33.31, 13.23, 41.31, 21.31, 452.2} // unsorted
	arrSlice := arr[:]
	sort.Sort(sort.Reverse(sort.Float64Slice(arrSlice)))
	fmt.Println("sorted array in descending order:", arr)
}

Output –

sorted array in descending order: [452.2 41.31 33.31 21.31 13.23 2.31]

We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at [email protected]

Liked the article? Share this on

Leave a Comment

Your email address will not be published.