×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang sort.Float64s() Function with Examples

Golang | sort.Float64s() Function: Here, we are going to learn about the Float64s() function of the sort package with its usages, syntax, and examples.
Submitted by IncludeHelp, on September 11, 2021

sort.Float64s()

The Float64s() function is an inbuilt function of the sort package which is used to sort a given slice of float64s (float64 type of elements) in increasing order (ascending order).

It accepts a parameter (x []float64) and returns nothing.

Note: NaN values are ordered before other values.

Syntax

func Float64s(x []float64)

Parameters

  • x : Slice of float64s to be sorted in ascending order.

Return Value

None

Example 1

// Golang program to demonstrate the
// example of sort.Float64s() Function

package main

import (
	"fmt"
	"math"
	"sort"
)

func main() {
	x := []float64{1.2, 8.9, 12.50, -10.0, 20.0, 15.5}
	fmt.Println("x (Before):", x)
	sort.Float64s(x)
	fmt.Println("x (After):", x)

	fmt.Println()

	x = []float64{1.2, 8.9, 0, math.Inf(-1), math.Inf(1), math.NaN()}
	fmt.Println("x (Before):", x)
	sort.Float64s(x)
	fmt.Println("x (After):", x)
}

Output:

x (Before): [1.2 8.9 12.5 -10 20 15.5]
x (After): [-10 1.2 8.9 12.5 15.5 20]

x (Before): [1.2 8.9 0 -Inf +Inf NaN]
x (After): [NaN -Inf 0 1.2 8.9 +Inf]

Example 2

// Golang program to demonstrate the
// example of sort.Float64s() Function

package main

import (
	"fmt"
	"math"
	"sort"
)

func main() {
	x := []float64{1.2, 8.9, 12.50, -10.0, 20.0, 15.5}
	// Printing x, type, sorting status
	fmt.Println("x:", x)
	fmt.Printf("%T, %v\n", x, sort.Float64sAreSorted(x))

	// Sorting x
	sort.Float64s(x)

	// Printing x, type, sorting status
	fmt.Println("x:", x)
	fmt.Printf("%T, %v\n", x, sort.Float64sAreSorted(x))

	fmt.Println()

	x = []float64{1.2, 8.9, 0, math.Inf(-1), math.Inf(1), math.NaN()}

	// Printing x, type, sorting status
	fmt.Println("x:", x)
	fmt.Printf("%T, %v\n", x, sort.Float64sAreSorted(x))

	// Sorting x
	sort.Float64s(x)

	// Printing x, type, sorting status
	fmt.Println("x:", x)
	fmt.Printf("%T, %v\n", x, sort.Float64sAreSorted(x))
}

Output:

x: [1.2 8.9 12.5 -10 20 15.5]
[]float64, false
x: [-10 1.2 8.9 12.5 15.5 20]
[]float64, true

x: [1.2 8.9 0 -Inf +Inf NaN]
[]float64, false
x: [NaN -Inf 0 1.2 8.9 +Inf]
[]float64, true

Golang sort Package »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.