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
+8.4k Golang : How to check variable or object type during runtime?
+16.5k Golang : File path independent of Operating System
+6k Golang : Build new URL for named or registered route with Gorilla webtoolkit example
+13.6k Golang : Tutorial on loading GOB and PEM files
+9.3k Golang : Generate EAN barcode
+17.6k Golang : delete and modify XML file content
+9.8k Golang : Function wrapper that takes arguments and return result example
+14.9k Golang : Search folders for file recursively with wildcard support
+52.4k Golang : How to get struct field and value by name
+8.9k Golang : Inject/embed Javascript before sending out to browser example
+35.2k Golang : Smarter Error Handling with strings.Contains()