Open In App

How to use Array Reverse Sort Functions for Integer and Strings in Golang?

Go language provides inbuilt support implementation of basic constants and run-time reflection to operate sort package. Golang is the ability for functions to run independently of each other. With the help of this function we can easily sort integer and string by importing “sort” package. Basically, this will sort the Integer and Strings in Reverse order.

Syntax:



func Reverse(data Interface) Interface

Return Value: This function returns the value of IntSlice and StringSlice value.

Below examples illustrate the use of the above method in Golang:



Example 1:




// Golang program to show the uses of
// Integer Reverse Sort Function
  
package main
  
import (
    "fmt"
    "sort"
)
  
func main() {
  
    fmt.Println("Example For Integer Reverse Sort")
  
    num := []int{70, 80, 20, 50, 10}
  
    // using the function
    sort.Sort(sort.Reverse(sort.IntSlice(num)))
    fmt.Println(num)
  
}

Output:

Example For Integer Reverse Sort
[80 70 50 20 10]

Example 2:




// Golang program to show the uses of
// String Reverse Sort Function
  
package main
  
import (
    "fmt"
    "sort"
)
  
func main() {
  
    fmt.Println("Example For String Reverse Sort")
  
    str:= []string{"GFG","Rank","India","Amid","Covid19"}
  
    // using the function
    sort.Sort(sort.Reverse(sort.StringSlice(str)))
    fmt.Println(str)
  
}

Output:

Example For String Reverse Sort
[Rank India GFG Covid19 Amid]

Article Tags :