Welcome To Golang By Example

Menu
  • Home
  • Blog
Menu

Sort a slice of Int in Ascending and Descending order in Go (Golang)

Posted on October 13, 2023October 13, 2023 by admin

Table of Contents

  • Sort a slice in Ascending order
  • Sort a slice in Descending order

Sort a slice in Ascending order

sort.Ints package of going can be used to sort a full slice or a part of the slice. It is going to sort the string into Ascending order.

Below is the signature of the method

https://pkg.go.dev/sort#Ints

func Ints(x []int)

It takes a slice ‘x’ as an argument and sorts the slice ‘x’ in place.

Below is the program for the same

package main
import (
"fmt"
"sort"
)
func main() {
nums := []int{3, 4, 2, 1}
fmt.Printf("Before: %v", nums)
sort.Ints(nums)
fmt.Printf("\nAfter: %v", nums)
}

Output

Before: [3 4 2 1]
After: [1 2 3 4]

Sort a slice in Descending order

To sort a slice in descending order we will use Slice method of sort pacakge

https://pkg.go.dev/sort#Slice

Below is the signature of the method

func Slice(x any, less func(i, j int) bool)

So this function takes two arguments

  • x any – any is nothing here but an empty interface https://pkg.go.dev/builtin#any
  • less func(i, j int) – This function is nothing but a comparator function

We can use this comparator function to decide the descending order of the elements in the slice. Below is an example

package main
import (
"fmt"
"sort"
)
func main() {
nums := []int{3, 4, 2, 1}
fmt.Printf("Before: %v", nums)
sort.Slice(nums, func(i, j int) bool {
return nums[i] > nums[j]
})
fmt.Printf("\nAfter: %v", nums)
}

Output

Before: [3 4 2 1]
After: [4 3 2 1]

As a matter of fact, you can use sort.Slice method to also sort a slice in descending order. Below is an example

package main
import (
"fmt"
"sort"
)
func main() {
nums := []int{3, 4, 2, 1}
fmt.Printf("Before: %v", nums)
sort.Slice(nums, func(i, j int) bool {
return nums[i] < nums[j]
})
fmt.Printf("\nAfter: %v", nums)
}

Output

Before: [3 4 2 1]
After: [1 2 3 4]

Note: Check out our Golang Advanced Tutorial. The tutorials in this series are elaborative and we have tried to cover all concepts with examples. This tutorial is for those who are looking to gain expertise and a solid understanding of golang - Golang Advance Tutorial

Also if you are interested in understanding how all design patterns can be implemented in Golang. If yes, then this post is for you -

All Design Patterns Golang

Note: Check out our system design tutorial series System Design Questions

  • go
  • Popular Articles

    Golang Comprehensive Tutorial Series

    All Design Patterns in Go (Golang)

    Slice in golang

    Variables in Go (Golang) – Complete Guide

    OOP: Inheritance in GOLANG complete guide

    Using Context Package in GO (Golang) – Complete Guide

    All data types in Golang with examples

    Understanding time and date in Go (Golang) – Complete Guide

    ©2023 Welcome To Golang By Example | Design: Web XP