Open In App

How to sort a slice of float64s in Golang?

In Go language slice is more powerful, flexible, convenient than an array, and is a lightweight data structure. The slice is a variable-length sequence which stores elements of a similar type, you are not allowed to store different type of elements in the same slice. Go language allows you to sort the elements of the slice according to its type. So, a float64 type slice is sorted by using the following functions. These functions are defined under the sort package so, you have to import sort package in your program for accessing these functions:

1. Float64s: This function is used to only sorts a slice of float64s and it sort the elements of the slice in increasing order.

Syntax:

func Float64s(slc []float64)

Here, slc represent a slice of float64. Let us discuss this concept with the help of an example:

Example:




// Go program to illustrate how
// to sort a slice of float64s
package main
  
import (
    "fmt"
    "sort"
)
  
// Main function
func main() {
  
    // Creating and initializing slices
    // Using shorthand declaration
    scl1 := []float64{9.56, 4.56, 2.4,
                 10, 43, 0.56, 35.246}
      
    scl2 := []float64{38.32, -32.23, 
               -45.56, 23.45, -0.43}
  
    // Displaying slices
    fmt.Println("Slice(Before):")
    fmt.Println("Slice 1: ", scl1)
    fmt.Println("Slice 2: ", scl2)
  
    // Sorting the elements of the slice
    // Using Float64s function
    sort.Float64s(scl1)
    sort.Float64s(scl2)
  
    // Displaying the result
    fmt.Println("\nSlice(After):")
    fmt.Println("Slice 1: ", scl1)
    fmt.Println("Slice 2: ", scl2)
}

Output:

Slice(Before):
Slice 1:  [9.56 4.56 2.4 10 43 0.56 35.246]
Slice 2:  [38.32 -32.23 -45.56 23.45 -0.43]

Slice(After):
Slice 1:  [0.56 2.4 4.56 9.56 10 35.246 43]
Slice 2:  [-45.56 -32.23 -0.43 23.45 38.32]

2. Float64sAreSorted: This function is used to check whether the given slice of float64s is in sorted form(in increasing order ) or not. This method returns true if the slice is in sorted form, or return false if the slice is not in the sorted form.

Syntax:

func Float64sAreSorted(scl []float64) bool

Here, scl represents a slice of float64s. Let us discuss this concept with the help of an example:

Example:




// Go program to illustrate how to check whether the
// given slice of float64 is in sorted form or not
package main
  
import (
    "fmt"
    "sort"
)
  
// Main function
func main() {
  
    // Creating and initializing slices
    // Using shorthand declaration
    scl1 := []float64{9.56, 4.56, 2.4,
                 10, 43, 0.56, 35.246}
      
    scl2 := []float64{-45.56, -32.23,
                 -0.43, 23.45, 38.32}
  
    // Displaying slices
    fmt.Println("Slices:")
    fmt.Println("Slice 1: ", scl1)
    fmt.Println("Slice 2: ", scl2)
  
    // Checking the slice is in sorted form or not
    // Using Float64sAreSorted  function
    res1 := sort.Float64sAreSorted(scl1)
    res2 := sort.Float64sAreSorted(scl2)
  
    // Displaying the result
    fmt.Println("\nResult:")
    fmt.Println("Is Slice 1 is sorted?: ", res1)
    fmt.Println("Is Slice 2 is sorted?: ", res2)
}

Output:

Slices:
Slice 1:  [9.56 4.56 2.4 10 43 0.56 35.246]
Slice 2:  [-45.56 -32.23 -0.43 23.45 38.32]

Result:
Is Slice 1 is sorted?:  false
Is Slice 2 is sorted?:  true

Article Tags :