Sorting a Slice of Integers in Reverse With Golang

Sorting a Slice of Integers in Reverse With Golang

Learn how to sort a slice of interger in golang using sort.Reverse()

Sorting a Slice of Integers in Reverse With Golang

Sorting a slice in golang can be done using the sort.Reverse() method from the sort. According to me, it is not the most straightforward way of doing the sort when compared to other languages. So let’s look at how to sort the slice of integers in golang first.

There are two methods to do this

Method 1: Using the sort.Reverse type - Recommended for simple integer slices

Method 2: Using the sort.Slice function - Recommended for custom type

Quick solution to sorting a slice of integers in Reverse (Method 1)

If you are in a hurry, below is the solution to reverse sort the slice of integers in Go. Please look at the detailed explanation to know why.

package main

import (
	"fmt"
	"sort"
)

func main() {
	availableRoomNum := []int{401, 305, 205, 103, 403, 402, 106}
	sort.Sort(sort.Reverse(sort.IntSlice(availableRoomNum)))
	fmt.Println(availableRoomNum)
}

Output

[403 402 401 305 205 106 103]

Things to Note

  1. It is an in-place sort meaning the variable availableRoomNum will be modified after the sorting.
  2. sort.IntSlice() function takes an int slice and helps in sorting in ascending order, then passing that to the sort.Reverse changes the order to descending sort.
  3. When there are equal elements, the sort function is not guaranteed to put those elements in the same place as before. If you want that, consider using sort.Stable instead of sort.Sort

Method 1 - Detailed explanation of sort package in Go

Before we dive into sorting the slice in reverse order, let’s look at how the Sort function works.

Sort() function definition

It takes data as an argument which is Interface and need to have these 3 methods defined for the sort to work.

  1. Len () - Gives the length of the slice
  2. Less (i, j) - The method takes in two indexes and determines if i element should be before j element.
  3. Swap (i, j) - Actual swap of i and j elements
func Sort(data [Interface](https://pkg.go.dev/sort#Interface))

type Interface interface {
	Len() int

	Less(i, j int) bool

	Swap(i, j int)
}

This means that any data variable passed to the sort should implement this interface and should have the implementation of these 3 functions. To make it easy for sorting the integer slice, Go already has an inbuild implementation type of this interface for integers, strings, float64 called sort.IntSlice() , sort.StringSlice(), sort.Float64Slice() respectively.

Step 1: Sorting a slice of integer in ascending order

So we can use that and pass our slice to the sort function to sort in ascending order

package main

import (
	"fmt"
	"sort"
)

func main() {
	availableRoomNum := []int{401, 305, 205, 103, 403, 402, 106}
	sort.Sort(sort.IntSlice(availableRoomNum))
	fmt.Println(availableRoomNum)
}

Output

[103 106 205 305 401 402 403]

Reverse() func definition

func Reverse(data Interface) Interface

It can take the same data variable as an argument and return the reversed order of the less function which is defined in the interface. So it returns the descending order of sorting when passing the ascending sort order as an argument.

Step 2: Sorting a slice of integer in descending order

package main

import (
	"fmt"
	"sort"
)

func main() {
	availableRoomNum := []int{401, 305, 205, 103, 403, 402, 106}
	sort.Sort(sort.Reverse(sort.IntSlice(availableRoomNum)))
	fmt.Println("Method 1: Sort Reverse", availableRoomNum)
}

Output

Method 1: Sort Reverse [403 402 401 305 205 106 103]

Method 2: Specifying the less function in Slice function in sort package

This method will let you pass the less function which we say in the previous method directly into the Slice method and gives you more control on how the sorting is achieved. This is useful if you want to have custom sorting logic as well.

package main

import (
	"fmt"
	"sort"
)

func main() {
	availableRoomNum := []int{401, 305, 205, 103, 403, 402, 106}

	reverseLessInt := func(i, j int) bool {
		return availableRoomNum[i] > availableRoomNum[j]
	}

	sort.Slice(availableRoomNum, reverseLessInt)

	fmt.Println("Method 2: Sort Reverse", availableRoomNum)
}

Output

Method 2: Sort Reverse [403 402 401 305 205 106 103]