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
+12.5k Golang : Tutorial on loading GOB and PEM files
+11.7k Golang : http.Get example
+5.1k PHP : How to check if an array is empty ?
+11.7k Android Studio : Password input and reveal password example
+7.9k Golang : Intercept and compare HTTP response code example
+10k Golang : Fix fmt.Scanf() on Windows will scan input twice problem
+18.4k Swift : Convert (cast) Int to int32 or Uint32
+7.8k Golang : Find duplicate files with filepath.Walk
+4.8k Unix/Linux : How to test user agents blocked successfully ?
+8.3k Golang : Changing a RGBA image number of channels with OpenCV
+17.4k Golang : Check whether a network interface is up on your machine