golang – how to sort string or []byte?

huangapple go评论118阅读模式
英文:

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:

  1. "bcad" to "abcd"
  2. or
  3. []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

  1. type sortRunes []rune
  2. func (s sortRunes) Less(i, j int) bool {
  3. return s[i] < s[j]
  4. }
  5. func (s sortRunes) Swap(i, j int) {
  6. s[i], s[j] = s[j], s[i]
  7. }
  8. func (s sortRunes) Len() int {
  9. return len(s)
  10. }
  11. func SortString(s string) string {
  12. r := []rune(s)
  13. sort.Sort(sortRunes(r))
  14. return string(r)
  15. }
  16. func main() {
  17. w1 := "bcad"
  18. w2 := SortString(w1)
  19. fmt.Println(w1)
  20. fmt.Println(w2)
  21. }
英文:

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

  1. type sortRunes []rune
  2. func (s sortRunes) Less(i, j int) bool {
  3. return s[i] &lt; s[j]
  4. }
  5. func (s sortRunes) Swap(i, j int) {
  6. s[i], s[j] = s[j], s[i]
  7. }
  8. func (s sortRunes) Len() int {
  9. return len(s)
  10. }
  11. func SortString(s string) string {
  12. r := []rune(s)
  13. sort.Sort(sortRunes(r))
  14. return string(r)
  15. }
  16. func main() {
  17. w1 := &quot;bcad&quot;
  18. w2 := SortString(w1)
  19. fmt.Println(w1)
  20. fmt.Println(w2)
  21. }

答案2

得分: 42

你可以将字符串转换为字符串切片,对其进行排序,然后再将其转换回字符串:

  1. package main
  2. import (
  3. "fmt"
  4. "sort"
  5. "strings"
  6. )
  7. func SortString(w string) string {
  8. s := strings.Split(w, "")
  9. sort.Strings(s)
  10. return strings.Join(s, "")
  11. }
  12. func main() {
  13. w1 := "bcad"
  14. w2 := SortString(w1)
  15. fmt.Println(w1)
  16. fmt.Println(w2)
  17. }

这段代码的输出结果是:

  1. bcad
  2. 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:

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;sort&quot;
  5. &quot;strings&quot;
  6. )
  7. func SortString(w string) string {
  8. s := strings.Split(w, &quot;&quot;)
  9. sort.Strings(s)
  10. return strings.Join(s, &quot;&quot;)
  11. }
  12. func main() {
  13. w1 := &quot;bcad&quot;
  14. w2 := SortString(w1)
  15. fmt.Println(w1)
  16. fmt.Println(w2)
  17. }

This prints:

  1. bcad
  2. abcd

Try it: http://play.golang.org/p/_6cTBAAZPb

答案3

得分: 30

有一种简单的方法可以利用函数sort.Slice来实现:

  1. package main
  2. import (
  3. "fmt"
  4. "sort"
  5. )
  6. func main() {
  7. word := "1BCagM9"
  8. s := []rune(word)
  9. sort.Slice(s, func(i int, j int) bool { return s[i] < s[j] })
  10. fmt.Println(string(s))
  11. }

Playground)

英文:

there is a simple way by leveraging function sort.Slice:

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;sort&quot;
  5. )
  6. func main() {
  7. word := &quot;1BCagM9&quot;
  8. s := []rune(word)
  9. sort.Slice(s, func(i int, j int) bool { return s[i] &lt; s[j] })
  10. fmt.Println(string(s))
  11. }

(Playground)

答案4

得分: 3

  1. package main
  2. import (
  3. "fmt"
  4. "sort"
  5. )
  6. func main() {
  7. word := "1&#224;ha漢字P&#233;py5"
  8. charArray := []rune(word)
  9. sort.Slice(charArray, func(i int, j int) bool {
  10. return charArray[i] < charArray[j]
  11. })
  12. fmt.Println(string(charArray))
  13. }

输出:

15Pahpyàé字漢

Playground

英文:
  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;sort&quot;
  5. )
  6. func main() {
  7. word := &quot;1&#224;ha漢字P&#233;py5&quot;
  8. charArray := []rune(word)
  9. sort.Slice(charArray, func(i int, j int) bool {
  10. return charArray[i] &lt; charArray[j]
  11. })
  12. fmt.Println(string(charArray))
  13. }

Output:

  1. 15Pahpy&#224;&#233;字漢

Playground

答案5

得分: 0

问题是,Golang没有一个方便的函数来对字符串进行排序。

  1. 使用int进行排序
    • 我认为需要进行太多的转换
    • 仍然使用rune将其合并为字符串
  1. func sortByInt(s string) string {
  2. var si = []int{}
  3. var sr = []rune{}
  4. for _, r := range s {
  5. si = append(si, int(r))
  6. }
  7. sort.Ints(si)
  8. for _, r := range si {
  9. sr = append(sr, rune(r))
  10. }
  11. return string(sr)
  12. }
  1. 为[]rune实现排序接口,只需记住
    • rune等于int32
    • byte等于uint8
  1. func sortBySlice(s string) []rune {
  2. sr := []rune(s)
  3. sort.Slice(sr, func(i int, j int) bool {
  4. return sr[i] < sr[j]
  5. })
  6. return sr
  7. }
英文:

the thing is, golang does not have a convenient function to sort string.

  1. Sort using int
  • too much conversion i think
  • still using rune to merge as string
  1. func sortByInt(s string) string {
  2. var si = []int{}
  3. var sr = []rune{}
  4. for _, r := range s {
  5. si = append(si, int(r))
  6. }
  7. sort.Ints(si)
  8. for _, r := range si {
  9. sr = append(sr, rune(r))
  10. }
  11. return string(sr)
  12. }
  1. Implement sort interface for []rune, just remember that
    • rune equals to int32
    • byte equals to uint8
  1. func sortBySlice(s string) []rune {
  2. sr := []rune(s)
  3. sort.Slice(sr, func(i int, j int) bool {
  4. return sr[i] &lt; sr[j]
  5. })
  6. return sr
  7. }

答案6

得分: 0

你可以像这样在Go语言中对字符串数组进行排序:

  1. // 文件名为 main.go
  2. package main
  3. import (
  4. "fmt"
  5. "sort"
  6. )
  7. /*
  8. * 这个函数用于对字符串数组进行排序
  9. */
  10. func main() {
  11. var names = []string{"b", "e", "a", "d", "g", "c", "f"}
  12. fmt.Println("原始字符串数组:", names)
  13. sort.Strings(names)
  14. fmt.Println("排序后的字符串数组:", names)
  15. return
  16. }

以上是对字符串数组进行排序的示例代码。

英文:

You can sort a string array in go like this

// name of the file is main.go

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;sort&quot;
  5. )
  6. /*
  7. *This function is used to sort a string array
  8. */
  9. func main() {
  10. var names = []string{&quot;b&quot;, &quot;e&quot;, &quot;a&quot;, &quot;d&quot;, &quot;g&quot;, &quot;c&quot;, &quot;f&quot;}
  11. fmt.Println(&quot;original string array: &quot;, names)
  12. sort.Strings(names)
  13. fmt.Println(&quot;After string sort array &quot;, names)
  14. return
  15. }

huangapple
  • 本文由 发表于 2014年3月27日 21:08:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/22688651.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定