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.3k Golang : Send data to /dev/null a.k.a blackhole with ioutil.Discard
+7.4k Gogland : Where to put source code files in package directory for rookie
+6.8k Golang : Levenshtein distance example
+6.4k Golang : Warp text string by number of characters or runes example
+40.7k Golang : How to check if a string contains another sub-string?
+14.3k Golang : Execute function at intervals or after some delay
+5.8k Linux/MacOSX : Search for files by filename and extension with find command
+21.1k Golang : How to read float value from standard input ?
+4.9k Swift : Convert (cast) Float to Int or Int32 value
+6.2k CodeIgniter : form input set_value cause " to become & quot
+21.9k Golang : Securing password with salt