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
+5.3k Golang : How to stop user from directly running an executable file?
+31.2k Golang : Validate IP address
+14.8k Golang : Multi threading or run two processes or more example
+10.2k Android Studio : Password input and reveal password example
+7.5k Golang : ffmpeg with os/exec.Command() returns non-zero status
+12.4k Golang : Search folders for file recursively with wildcard support
+6.3k Golang : How to check if input string is a word?
+6k Javascript : How to check a browser's Do Not Track status?
+24.2k Golang : Encrypt and decrypt data with AES crypto
+4.4k Golang & Javascript : How to save cropped image to file on server
+8.9k Linux : How to install driver for 600Mbps Dual Band Wifi USB Adapter
+22.3k Golang : Call function from another package