Golang Program To Sort The Elements Of An Array In Descending Order


In this tutorial, we will see to write a go language program to sort an array in descending order. In mathematics, a descending order is an order in which the following element is smaller than the previous element.

To Sort An Array In Descending Order Using External Functions

In this example, we will see to write a program to sort an array of integers in descending order using user defined function.

Algorithm

Step 1 − Import the fmt package

Step 2 − Define a function sortDesc() to sort the given array. This function accepts one argument as the array of integers that we wish to sort.

Step 3 − The function uses then two for loops and if condition to sort the array.

Step 4 − Use the first for loop to iterate over the unsorted array and the second for loop to get the min value present in the array.

Step 5 − Then use a temporary variable to put the smaller value after the larger one.

Step 6 − Start the main() function.

Step 7 − Initialize an array of integers and store values to it. print the unsorted array on the screen.

Step 8 − Then we need to call the sortDesc() function by passing the array to be sorted as argument to the function.

Step 9 − Store the array returned by the function and print it on the screen using fmt.Println() function.

Example

The following code illustrates how we can sort an array of elements in descending order in go programming language.

package main
import "fmt"
func sortDesc(arr [5]int) [5]int {
   for i := 0; i < len(arr); i++ {
   for j := 1 + i; j < len(arr); j++ {
         if arr[i] < arr[j] {
            intermediate := arr[i]
            arr[i] = arr[j]
            arr[j] = intermediate
         }
      }
   }
   return arr
}
func main() {
   arr := [5]int{50, 30, 20, 10, 40}
   fmt.Println("The unsorted array entered is:", arr)
   array := sortDesc(arr)
   fmt.Println()
   fmt.Println("The final array obtained after sorting is:", array)
}

Output

The unsorted array entered is: [50 30 20 10 40]

The final array obtained after sorting is: [50 40 30 20 10]

Sort An Array In Descending Order Using Intslice() Function

The following code illustrates to sort an array of integers in descending order in go programming language using pre-defined functions.

Syntax

sort.Sort(sort.Reverse(sort.IntSlice(arr)))

The IntSlice() function is present in the sort package and is used to sort an array of integers. The function takes an array of integers in unsorted format and returns the array by sorting it.The Reverse() function is present in the sort package. This function takes the data to be sorted in reverse order as an argument and returns the slice of the array by reversing its values.

Algorithm

Step 1 − Import the fmt and sort packages.

Step 2 − Start the main() function.

Step 3 − Initialize an array of integers and store values to it. print the unsorted array on the screen.

Step 4 − Now we need to call the function Ints() present in the sort package by passing the array to be sorted as an argument to the function.

Step 5 − The arr array is now sorted. We can print it on the screen using fmt.Println() function.

Example

package main
import (
   "fmt"
   "sort"
)
func main() {
   var arr = []int{9, 8, 7, 4, 5, 3}
   fmt.Println("Unsorted array of strings is", arr)
   sort.Sort(sort.Reverse(sort.IntSlice(arr)))
   fmt.Println("The above array is sorted and the result is:", arr)
}

Output

Unsorted array of strings is [9 8 7 4 5 3]
The above array is sorted and the result is: [9 8 7 5 4 3]

Sort An Array Of Strings In Descending Order Using The Stringslice() Method

Let us now write a program to sort an array of strings in descending order using predefined functions in go programming language.

Syntax

sort.Sort(sort.Reverse(sort.StringSlice(arr)))

The StringSlice() function is present in the sort package and it takes the array of strings to be sorted as an argument and returns the sorted string. The Reverse() function is present in the sort package. This function takes the data to be sorted in reverse order as an argument and returns the slice of the array by reversing its values.

Algorithm

Step 1 − Import the fmt and sort packages.

Step 2 − Start the main() function.

Step 3 − Initialize an array of strings and store values to it. print the unsorted array on the screen.

Step 4 − Now we need to call the function StringSlice() function present in the sort package by passing the array to be sorted as an argument to the function.

Step 5 − The arr array is now sorted. We can print it on the screen using fmt.Println() function.

Example

package main
import (
   "fmt"
   "sort"
)
func main() {
   var arr = []string{"a", "b", "c", "d", "s"}
   fmt.Println("Unsorted array of strings is", arr)
   sort.Sort(sort.Reverse(sort.StringSlice(arr)))
   fmt.Println("The above array is sorted and the result is:", arr)
}

Output

Unsorted array of strings is [a b c d s]
The above array is sorted and the result is: [s d c b a]

Conclusion

We have successfully compiled and executed a go language program to sort an array in descending order along with examples.

Updated on: 02-Jan-2023

392 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements