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
+7.4k Golang : Fixing Gorilla mux http.FileServer() 404 problem
+5.5k Unix/Linux : How to archive and compress entire directory ?
+5.3k Linux/Unix/MacOSX : Find out which application is listening to port 80 or use which IP version
+21.2k Golang : Sort and reverse sort a slice of strings
+12.9k Swift : Convert (cast) Int or int32 value to CGFloat
+32.3k Golang : Validate email address with regular expression
+11.7k Get form post value in Go
+5.9k CodeIgniter/PHP : Remove empty lines above RSS or ATOM xml tag
+16.6k Golang : Check if a string contains multiple sub-strings in []string?
+14.7k Golang : How to get URL port?
+30.5k Golang : How to redirect to new page with net/http?
+7.3k Javascript : How to get JSON data from another website with JQuery or Ajax ?