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
+6.2k Golang : Inject/embed Javascript before sending out to browser example
+11.3k Golang : Fix cannot convert buffer (type *bytes.Buffer) to type string error
+8.5k Golang : Get absolute path to binary for os.Exec function with exec.LookPath
+4.1k Javascript : Push notifications to browser with Push.js
+2.5k Javascript : How to show different content with noscript?
+5.9k Golang : automatically figure out array length(size) with three dots
+29.5k Golang : Proper way to set function argument default value
+18.3k Golang : Securing password with salt
+6.4k Golang : Random integer with rand.Seed() within a given range
+10.4k Golang : GUI with Qt and OpenCV to capture image from camera
+4.2k Golang : How to iterate a slice without using for loop?
+29.2k Golang : Convert date or time stamp from string to time.Time type