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
+17.6k Golang : Check if IP address is version 4 or 6
+48.2k Golang : Convert int to byte array([]byte)
+16.5k Golang : Find out mime type from bytes in buffer
+13.9k Golang : Activate web camera and broadcast out base64 encoded images
+5.3k Linux/Unix/MacOSX : Find out which application is listening to port 80 or use which IP version
+14.9k Golang : Normalize unicode strings for comparison purpose
+5.9k Linux/Unix/PHP : Restart PHP-FPM
+10.6k Golang : Simple Jawi(Yawi) to Rumi(Latin/Romanize) converter
+20.8k Nginx + FastCGI + Go Setup.
+7k Golang : Pat multiplexer routing example
+5.4k Golang : Print instead of building pyramids
+10.4k Golang : Convert file content to Hex