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
+11.5k Golang : Concatenate (combine) buffer data example
+9k Golang : Find network service name from given port and protocol
+29.5k Golang : JQuery AJAX post data to server and send data back to client example
+23.1k Golang : Calculate time different
+22.2k Golang : Join arrays or slices example
+17.5k Golang : Multi threading or run two processes or more example
+7.1k Golang : Takes a plural word and makes it singular
+9.3k Golang : How to find out similarity between two strings with Jaro-Winkler Distance?
+7.2k Nginx : How to block user agent ?
+22.3k Golang : Securing password with salt
+12.2k Golang : Save webcamera frames to video file
+8.8k Golang : How to join strings?