How to sort a slice in Golang?

We have already sorted an array in one of our previous articles and in this post, we are going to sort the slice using some inbuilt functions provided by the Go language.

We will be looking at the below points in this post.

Let’s look at all of the above points one by one.

Sorting a slice of ints in ascending order

Here, we will be using the Ints function of the sort package to sort a slice of integers in ascending order.

package main

import (
	"fmt"
	"sort"
)

func main() {
	arrSlice := []int{87, 32, 57, 82, 23, 62} // unsorted slice
	sort.Ints(arrSlice)
	fmt.Println("sorted slice in ascending order:", arrSlice)
}

Output –

sorted slice in ascending order: [23 32 57 62 82 87]

Internal implementation of the Ints method

func Ints(x []int) { Sort(IntSlice(x)) }

Here, the slice will be wrapped up in an IntSlice which attaches the Less, Len, and Swap methods that will be used by the Sort() method to actually sort the slice.

Sorting a slice of ints in descending order

package main

import (
	"fmt"
	"sort"
)

func main() {
	arrSlice := []int{87, 32, 57, 82, 23, 62} // unsorted slice
	sort.Sort(sort.Reverse(sort.IntSlice(arrSlice)))
	fmt.Println("sorted slice in descending order:", arrSlice)
}

Output –

sorted slice in descending order: [87 82 62 57 32 23]

So, what happened here?

  • First, we again wrapped our slice in an IntSlice, which attaches the Less, Swap, and Len method used for sorting the slice
  • Then, the sort.Reverse() will reverse the implementation of the Less method
  • And finally, sort.Sort() will try to sort the slice but as the implementation of the Less method is reversed, the slice of integers will be sorted in the reverse order

Now, let’s try to sort the slice containing elements of type float64. All of the implementations will remain the same with a difference that we will be wrapping up our slice in the Float64Slice now instead of the IntSlice.

Sorting a slice of float64 type elements in ascending order

Here, now, we will be using the Float64s function of the sort package.

package main

import (
	"fmt"
	"sort"
)

func main() {
	arrSlice := []float64{87.13, 87.50, 32, 57.3, 82, 23, 62.87} // unsorted slice
	sort.Float64s(arrSlice)
	fmt.Println("sorted slice in ascending order:", arrSlice)
}

Output –

sorted slice in ascending order: [23 32 57.3 62.87 82 87.13 87.5]

Internal implementation of the Float64s method

func Float64s(x []float64) { Sort(Float64Slice(x)) }

Sorting a slice of float64 type elements in descending order

package main

import (
	"fmt"
	"sort"
)

func main() {
	arrSlice := []float64{87.13, 87.50, 32, 57.3, 82, 23, 62.87} // unsorted slice
	sort.Sort(sort.Reverse(sort.Float64Slice(arrSlice)))
	fmt.Println("sorted slice in descending order:", arrSlice)
}

Output –

sorted slice in descending order: [87.5 87.13 82 62.87 57.3 32 23]

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