英文:
golang - how to sort string or []byte?
问题
我正在寻找一个可以对string
或[]byte
进行排序的函数:
将字符串"bcad"
排序为"abcd"
,
或者将[]byte("bcad")
排序为[]byte("abcd")
。
字符串中只包含字母,但排序应该适用于字母和数字。
我找到了sort
包,但没有我想要的函数。
英文:
I am looking for a function, that can sort string
or []byte
:
"bcad" to "abcd"
or
[]byte("bcad") to []byte("abcd")
The string only contains letters - but sorting should also work for letters and numbers.
I found sort package but not the function I want.
答案1
得分: 45
创建每个字符的字符串然后将它们连接起来感觉很浪费。
这里有一个稍微节省一些的方法,但是有更多的样板代码。playground://XEckr_rpr8
type sortRunes []rune
func (s sortRunes) Less(i, j int) bool {
return s[i] < s[j]
}
func (s sortRunes) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s sortRunes) Len() int {
return len(s)
}
func SortString(s string) string {
r := []rune(s)
sort.Sort(sortRunes(r))
return string(r)
}
func main() {
w1 := "bcad"
w2 := SortString(w1)
fmt.Println(w1)
fmt.Println(w2)
}
英文:
It feels wasteful to create a string for each character just to Join
them.
Here's one that is a little less wasteful, but with more boiler plate. playground://XEckr_rpr8
type sortRunes []rune
func (s sortRunes) Less(i, j int) bool {
return s[i] < s[j]
}
func (s sortRunes) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s sortRunes) Len() int {
return len(s)
}
func SortString(s string) string {
r := []rune(s)
sort.Sort(sortRunes(r))
return string(r)
}
func main() {
w1 := "bcad"
w2 := SortString(w1)
fmt.Println(w1)
fmt.Println(w2)
}
答案2
得分: 42
你可以将字符串转换为字符串切片,对其进行排序,然后再将其转换回字符串:
package main
import (
"fmt"
"sort"
"strings"
)
func SortString(w string) string {
s := strings.Split(w, "")
sort.Strings(s)
return strings.Join(s, "")
}
func main() {
w1 := "bcad"
w2 := SortString(w1)
fmt.Println(w1)
fmt.Println(w2)
}
这段代码的输出结果是:
bcad
abcd
你可以在这里尝试运行代码:http://play.golang.org/p/_6cTBAAZPb
英文:
You can convert the string to a slice of strings, sort it, and then convert it back to a string:
package main
import (
"fmt"
"sort"
"strings"
)
func SortString(w string) string {
s := strings.Split(w, "")
sort.Strings(s)
return strings.Join(s, "")
}
func main() {
w1 := "bcad"
w2 := SortString(w1)
fmt.Println(w1)
fmt.Println(w2)
}
This prints:
bcad
abcd
答案3
得分: 30
有一种简单的方法可以利用函数sort.Slice
来实现:
package main
import (
"fmt"
"sort"
)
func main() {
word := "1BCagM9"
s := []rune(word)
sort.Slice(s, func(i int, j int) bool { return s[i] < s[j] })
fmt.Println(string(s))
}
英文:
there is a simple way by leveraging function sort.Slice
:
package main
import (
"fmt"
"sort"
)
func main() {
word := "1BCagM9"
s := []rune(word)
sort.Slice(s, func(i int, j int) bool { return s[i] < s[j] })
fmt.Println(string(s))
}
答案4
得分: 3
package main
import (
"fmt"
"sort"
)
func main() {
word := "1àha漢字Pépy5"
charArray := []rune(word)
sort.Slice(charArray, func(i int, j int) bool {
return charArray[i] < charArray[j]
})
fmt.Println(string(charArray))
}
输出:
15Pahpyàé字漢
英文:
package main
import (
"fmt"
"sort"
)
func main() {
word := "1àha漢字Pépy5"
charArray := []rune(word)
sort.Slice(charArray, func(i int, j int) bool {
return charArray[i] < charArray[j]
})
fmt.Println(string(charArray))
}
Output:
15Pahpyàé字漢
答案5
得分: 0
问题是,Golang没有一个方便的函数来对字符串进行排序。
- 使用int进行排序
- 我认为需要进行太多的转换
- 仍然使用rune将其合并为字符串
func sortByInt(s string) string {
var si = []int{}
var sr = []rune{}
for _, r := range s {
si = append(si, int(r))
}
sort.Ints(si)
for _, r := range si {
sr = append(sr, rune(r))
}
return string(sr)
}
- 为[]rune实现排序接口,只需记住
rune
等于int32
byte
等于uint8
func sortBySlice(s string) []rune {
sr := []rune(s)
sort.Slice(sr, func(i int, j int) bool {
return sr[i] < sr[j]
})
return sr
}
英文:
the thing is, golang does not have a convenient function to sort string.
- Sort using int
- too much conversion i think
- still using rune to merge as string
func sortByInt(s string) string {
var si = []int{}
var sr = []rune{}
for _, r := range s {
si = append(si, int(r))
}
sort.Ints(si)
for _, r := range si {
sr = append(sr, rune(r))
}
return string(sr)
}
- Implement sort interface for []rune, just remember that
rune
equals toint32
- byte equals to
uint8
func sortBySlice(s string) []rune {
sr := []rune(s)
sort.Slice(sr, func(i int, j int) bool {
return sr[i] < sr[j]
})
return sr
}
答案6
得分: 0
你可以像这样在Go语言中对字符串数组进行排序:
// 文件名为 main.go
package main
import (
"fmt"
"sort"
)
/*
* 这个函数用于对字符串数组进行排序
*/
func main() {
var names = []string{"b", "e", "a", "d", "g", "c", "f"}
fmt.Println("原始字符串数组:", names)
sort.Strings(names)
fmt.Println("排序后的字符串数组:", names)
return
}
以上是对字符串数组进行排序的示例代码。
英文:
You can sort a string array in go like this
// name of the file is main.go
package main
import (
"fmt"
"sort"
)
/*
*This function is used to sort a string array
*/
func main() {
var names = []string{"b", "e", "a", "d", "g", "c", "f"}
fmt.Println("original string array: ", names)
sort.Strings(names)
fmt.Println("After string sort array ", names)
return
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论