golang – how to sort string or []byte?

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

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] &lt; 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 := &quot;bcad&quot;
	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 (
	&quot;fmt&quot;
	&quot;sort&quot;
	&quot;strings&quot;
)

func SortString(w string) string {
	s := strings.Split(w, &quot;&quot;)
	sort.Strings(s)
	return strings.Join(s, &quot;&quot;)
}

func main() {
	w1 := &quot;bcad&quot;
	w2 := SortString(w1)

	fmt.Println(w1)
	fmt.Println(w2)
}

This prints:

bcad
abcd

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

答案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))
}

Playground)

英文:

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

package main

import (
	&quot;fmt&quot;
	&quot;sort&quot;
)

func main() {
	word := &quot;1BCagM9&quot;
    s := []rune(word)
	sort.Slice(s, func(i int, j int) bool { return s[i] &lt; s[j] })
	fmt.Println(string(s))
}

(Playground)

答案4

得分: 3

package main
import (
    "fmt"
    "sort"
)
func main() {
    word := "1&#224;ha漢字P&#233;py5"
    charArray := []rune(word)
    sort.Slice(charArray, func(i int, j int) bool {
        return charArray[i] < charArray[j]
    })
    fmt.Println(string(charArray))
}

输出:

15Pahpyàé字漢

Playground

英文:
package main
import (
    &quot;fmt&quot;
    &quot;sort&quot;
)
func main() {
    word := &quot;1&#224;ha漢字P&#233;py5&quot;
    charArray := []rune(word)
    sort.Slice(charArray, func(i int, j int) bool {
        return charArray[i] &lt; charArray[j]
    })
    fmt.Println(string(charArray))
}

Output:

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

Playground

答案5

得分: 0

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

  1. 使用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)
}
  1. 为[]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.

  1. 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)
}

  1. Implement sort interface for []rune, just remember that
    • rune equals to int32
    • byte equals to uint8
func sortBySlice(s string) []rune {
	sr := []rune(s)
	sort.Slice(sr, func(i int, j int) bool {
		return sr[i] &lt; 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 (
	&quot;fmt&quot;
	&quot;sort&quot;
)

/*
*This function is used to sort  a string array
 */
func main() {
	var names = []string{&quot;b&quot;, &quot;e&quot;, &quot;a&quot;, &quot;d&quot;, &quot;g&quot;, &quot;c&quot;, &quot;f&quot;}
	fmt.Println(&quot;original string array:   &quot;, names)
	sort.Strings(names)
	fmt.Println(&quot;After string sort array &quot;, names)
	return
}

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:

确定