Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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

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

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

(Playground)

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àé字漢

Playground

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] < sr[j]
    })
    return sr
}

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
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top