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:



filter_none

edit
close

play_arrow

link
brightness_4
code

// 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)
  
}

chevron_right


Output:

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

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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)
  
}

chevron_right


Output:

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



My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.