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
+11.7k Golang : Display a text file line by line with line number example
+17.6k Golang : Find smallest number in array
+7.4k Golang : Convert source code to assembly language
+5.9k Golang : Detect variable or constant type
+7.1k Nginx : How to block user agent ?
+8.2k Golang : Check from web if Go application is running or not
+12k Golang : Convert decimal number(integer) to IPv4 address
+29.9k Golang : How to get HTTP request header information?
+6.8k Golang : Experimental emojis or emoticons icons programming language
+22.9k Golang : untar or extract tar ball archive example
+5.2k Golang : Print instead of building pyramids
+14k Golang : convert rune to unicode hexadecimal value and back to rune character