Golang : Sort and reverse sort a slice of floats
Problem :
How to sort and reverse sort a slice of floats ?
Solution :
Declare the slice type as sort.Float64Slice and use Sort() method and sort.Reverse functions.
package main
import (
"fmt"
"sort"
)
var floatSlice sort.Float64Slice = []float64{4.22222, 1.5555, -6.55555, 99.889888}
func main() {
fmt.Println("Original : ", floatSlice[:])
floatSlice.Sort()
fmt.Println("Sort : ", floatSlice[:])
sort.Sort(sort.Reverse(floatSlice[:]))
fmt.Println("Reverse : ", floatSlice[:])
}
Output :
Original : [4.22222 1.5555 -6.55555 99.889888]
Sort : [-6.55555 1.5555 4.22222 99.889888]
Reverse : [99.889888 4.22222 1.5555 -6.55555]
See also : Golang : Sort and reverse sort a slice of integers
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+10.4k Golang : Meaning of omitempty in struct's field tag
+17.1k Golang : Find file size(disk usage) with filepath.Walk
+14.4k Golang : How to filter a map's elements for faster lookup
+5.7k List of Golang XML tutorials
+7.1k Golang : Gorrila mux.Vars() function example
+15k Golang : How do I get the local IP (non-loopback) address ?
+9.2k Golang : How to get username from email address
+19.7k Golang : Measure http.Get() execution time
+9.9k Golang : Translate language with language package example
+28.7k Golang : Detect (OS) Operating System
+9.8k Golang : Function wrapper that takes arguments and return result example