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.1k Golang : Trim everything onward after a word
+4k Golang : Convert lines of string into list for delete and insert operation
+17.2k Golang : Generate thumbnails from images
+5.9k Golang : Levenshtein distance example
+24.2k Golang : How to read integer value from standard input ?
+7.3k Android Studio : Import third-party library or package into Gradle Scripts
+18.5k Golang : Reset or rewind io.Reader or io.Writer
+6.5k Golang : Handling Yes No Quit query input
+13.1k Golang : Reset buffer example
+13.9k Golang : Force download file example
+6.5k SSL : How to check if current certificate is sha1 or sha2 from command line
+3.9k Which content-type(MIME type) to use for JSON data