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
+36.5k Golang : Comparing date or timestamp
+6.9k Golang : Gomobile init produce "iphoneos" cannot be located error
+6.7k Golang : How to execute code at certain day, hour and minute?
+5.7k PHP : Shuffle to display different content or advertisement
+18.2k Golang : Example for DSA(Digital Signature Algorithm) package functions
+7.7k Golang : Gorilla web tool kit schema example
+29.6k error: trying to remove "yum", which is protected
+5.8k Golang : Muxing with Martini example
+10.6k Golang : Convert decimal number(integer) to IPv4 address
+21.4k Golang : simulate tail -f or read last line from log file example
+5.9k Golang : Decode XML data from RSS feed