Golang : Sort and reverse sort a slice of bytes
Problem :
How to sort and reverse sort a slice of bytes ?
Solution :
Import "github.com/cznic/sortutil"
Use the ByteSlice type ( see http://godoc.org/github.com/cznic/sortutil#ByteSlice ) and invoke the Sort() method.
package main
import (
"fmt"
"github.com/cznic/sortutil"
"sort"
)
var bytes sortutil.ByteSlice = []byte("zxvfbac")
func main() {
// Bytes
fmt.Println("Original : ", string(bytes[:]))
fmt.Println("Original : ", bytes[:])
sort.Sort(bytes)
fmt.Println("Sort : ", string(bytes[:]))
fmt.Println("Sort : ", bytes[:])
sort.Sort(sort.Reverse(bytes[:]))
fmt.Println("Reverse : ", string(bytes[:]))
fmt.Println("Reverse : ", bytes[:])
}
Output :
Original : zxvfbac
Original : [122 120 118 102 98 97 99]
Sort : abcfvxz
Sort : [97 98 99 102 118 120 122]
Reverse : zxvfcba
Reverse : [122 120 118 102 99 98 97]
See also : Golang : Sort and reverse sort a slice of runes
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
+6.2k Golang : Scan forex opportunities by Bollinger bands
+10.1k Golang : Channels and buffered channels examples
+8.3k Golang : Check from web if Go application is running or not
+11k Golang : Sieve of Eratosthenes algorithm
+17.7k Golang : Upload/Receive file progress indicator
+5.5k Golang : fmt.Println prints out empty data from struct
+8.4k Golang: Prevent over writing file with md5 hash
+5.5k Python : Delay with time.sleep() function example
+10k Golang : ffmpeg with os/exec.Command() returns non-zero status
+6.8k Golang : Reverse by word
+13.3k Golang : How to calculate the distance between two coordinates using Haversine formula
+8.2k Golang : Variadic function arguments sanity check example