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
+15k Golang :Trim white spaces from a string
+29.6k Golang : Convert an image file to []byte
+22k Golang : Fix type interface{} has no field or no methods and type assertions example
+8.3k Golang : Get all countries currencies code in JSON format
+11.9k Golang : Linear algebra and matrix calculation example
+16.3k Golang : How to log each HTTP request to your web server?
+11.3k Swift : Convert (cast) Int or int32 value to CGFloat
+5.7k Golang : Reverse by word
+5.1k Linux/Unix : Commands that you need to be careful about
+5.2k Golang : Build new URL for named or registered route with Gorilla webtoolkit example
+18.2k Golang : Check if os.Stdin input data is piped or from terminal
+15.5k Golang : Capture stdout of a child process and act according to the result