SOLVED: How to reverse sort slice of integer in GO


Written by - Tuan Nguyen
Reviewed by - Deepak Prasad

I. Brief Overview

This article will cover the Slice sort and reverse functions present in Golang, especially how to sort a slice of integer in reverse or descending order.

In Go, there are some functions such as sort.Ints(), sort.Strings(), or sort.Float64s() to help you sort a slice of a simple type ([]int, []string, or []float64). However, these functions only sort a slice in ascending order.

  • To sort in descending order, you have to implement the sort.Interface interface with the data reversal function sort.Reverse().
  • Another way to reverse sort a slice of integer is using sort.Slice() function.

All of the above functions and interfaces are described in below table:

Name Description
func Ints(x []int) Ints sorts a slice of ints in increasing order.
func Sort(data Interface) Sort sorts data in ascending order as determined by the Less method. It makes one call to data.Len to determine n and O(n*log(n)) calls to data.Less and data.Swap. The sort is not guaranteed to be stable.
Interface sort.Interface An implementation of Interface can be sorted by the routines in this package. The methods refer to elements of the underlying collection by integer index.
func Reverse(data Interface) Interface Reverse returns the reverse order for data. (redefines the Less method)
type IntSlice []int IntSlice attaches the methods of Interface to []int, sorting in increasing 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 less function must satisfy the same requirements as the Interface type's Less method.

 

II. Method 1: Using sort.IntSlice and sort.Reverse

In this example, we will combine sort.IntSlice, sort.Reverse, sort.Sort to reverse sort a slice of integer

package main

import (
	"fmt"
	"sort"
)

func main() {
	numbers := []int{4, 3, 2, 1, 0, 4, 7, 5}
	// sort ints ascending
	sort.Ints(numbers)
	fmt.Println(numbers)

	// sort ints descending
	sort.Sort(sort.Reverse(sort.IntSlice(numbers)))
	fmt.Println(numbers)
}

Output:

[0 1 2 3 4 4 5 7]
[7 5 4 4 3 2 1 0]

Explanation:

  1. Convert the []int slice to sort.IntSlice, which makes the slice an instance of the sort.Interface interface.
  2. Reverse the standard ascending order of the elements included in the sort.IntSlice by using the sort.Reverse() function.
  3. Sort the reversed slice using the general sort.Sort() function.

 

III. Method 2: Using sort.Slice

In this example, sort.Slice will be used to sort the slice of integer in descending order where we perform a comparison of each integer to determine the highest and lowest value and then sort them.

package main

import (
	"fmt"
	"sort"
)

func main() {
	integers := []int{9, 10, 11, 15, 24}
	sort.Slice(integers, func(x, y int) bool {
		return integers[y] < integers[x]
	})
	fmt.Println(integers)
}

Output:

[24 15 11 10 9]

Explanation: Sort the given slice ‘integers’ with the less function in which priority is given to larger numbers.

 

IV. Summary

In this article, I have demonstrated two methods for sorting a sequence of integers in ascending and reverse or descending order. We can use the second method to sort data of any other type in the desired order.

 

V. References

https://pkg.go.dev/sort#IntSlice
https://pkg.go.dev/sort#Slice
https://pkg.go.dev/sort#Interface
https://pkg.go.dev/sort#Reverse
https://pkg.go.dev/sort#Sort

 

Views: 2

Tuan Nguyen

He is proficient in Golang, Python, Java, MongoDB, Selenium, Spring Boot, Kubernetes, Scrapy, API development, Docker, Data Scraping, PrimeFaces, Linux, Data Structures, and Data Mining. With expertise spanning these technologies, he develops robust solutions and implements efficient data processing and management strategies across various projects and platforms. You can connect with him on LinkedIn.

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.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment