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
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)
}
[403 402 401 305 205 106 103]
Things to Note
availableRoomNum
will be modified after the sorting.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.sort.Stable
instead of sort.Sort
Before we dive into sorting the slice in reverse order, let’s look at how the Sort
function works.
It takes data as an argument which is Interface
and need to have these 3 methods defined for the sort to work.
i
element should be before j
element.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.
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)
}
[103 106 205 305 401 402 403]
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.
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)
}
Method 1: Sort Reverse [403 402 401 305 205 106 103]
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)
}
Method 2: Sort Reverse [403 402 401 305 205 106 103]
Sriram is a content creator focused on providing quality content which provides value for the readers. He believe is constant learning and sharing those learning with the group. He is working as a lead developer and bulk of his experience is in working with multiple javascript frameworks such as Angular, React, Svelte. He is passionate about open source technologies and on his free time loves to develop games.
We will reach out when exciting new posts are available. We won’t send you spam. Unsubscribe at any time.