Created
June 29, 2015 20:33
-
-
Save jaekwon/76bcb80081ce39b1588d to your computer and use it in GitHub Desktop.
SortString in Golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"sort" | |
"testing" | |
) | |
// SortString sorts a string | |
func SortString(w string) string { | |
wBytes := uint8Slice([]uint8(w)) | |
sort.Sort(wBytes) | |
return string(wBytes) | |
} | |
func main() { | |
for i := 0; i < 1000000; i++ { | |
s1 := "sfasdfasfsafsfasdbmziwomzpwgjamzp485t9zmzpabcdefg" | |
s2 := "sfasdfasffsdf02mzsafsfasdbmziwomzpwgjamzp485t9zmzpabcdefg" | |
sorted1 := SortString(s1) | |
sorted2 := SortString(s2) | |
if sorted1 == sorted2 { | |
//fmt.Println("yes") | |
} else { | |
//fmt.Println("no") | |
} | |
} | |
} | |
// Sort interface for []uint8 | |
type uint8Slice []uint8 | |
func (p uint8Slice) Len() int { return len(p) } | |
func (p uint8Slice) Less(i, j int) bool { return p[i] < p[j] } | |
func (p uint8Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } | |
func (p uint8Slice) Sort() { sort.Sort(p) } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment