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
+4.5k Golang : Frobnicate or tweaking a string example
+5.3k Javascript : Generate random key with specific length
+12.7k Golang : Fix image: unknown format error
+10.4k Golang : Fuzzy string search or approximate string matching example
+3.8k Facebook : How to place save to Facebook button on your website
+6.8k Golang : Reverse a string with unicode
+11.2k Golang : Add ASCII art to command line application launching process
+12.6k Javascript : Prompt confirmation before exit
+3.9k Python : Find out the variable type and determine the type with simple test
+4.3k Golang : Calculate half life decay example
+8.3k Golang : Detect number of active displays and the display's resolution
+17.8k Golang : When to use public and private identifier(variable) and how to make the identifier public or private?