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
+9.2k Golang : Convert file content to Hex
+6.4k Golang : Create zip/ePub file without compression(use Store algorithm)
+14.6k Golang : Read large file with bufio.Scanner cause token too long error
+4.3k Python : Convert(cast) string to bytes example
+7.6k Golang : Progress bar with ∎ character
+5.7k Elasticsearch : Shutdown a local node
+5.8k Golang : Check if password length meet the requirement
+4.6k Unix/Linux : How to find out the hard disk size?
+8.2k Golang : Timeout example
+26.5k Golang : Move file to another directory
+14.7k Golang : How to reverse elements order in map ?
+21.1k Golang : Repeat a character by multiple of x factor