Sort the letters of string in alphabetical order: Difference between revisions

Added Go
(→‎{{header|Wren}}: Oops, lost a line!)
(Added Go)
Line 6:
Write the function even your language has a built-in function for it.
<br><br>
 
=={{header|Go}}==
As in the case of the Wren entry, we write a function to bubble sort the characters of a string since this method is not, of course, used in Go's standard 'sort' package.
<lang go>package main
 
import (
"fmt"
"strings"
)
 
func bubbleSort(s string) string {
chars := []rune(s)
n := len(chars)
for {
n2 := 0
for i := 1; i < n; i++ {
if chars[i-1] > chars[i] {
tmp := chars[i]
chars[i] = chars[i-1]
chars[i-1] = tmp
n2 = i
}
}
n = n2
if n == 0 {
break
}
}
return string(chars)
}
 
func main() {
s := "forever go programming language"
s = bubbleSort(s)
s = strings.TrimLeft(s, " \t") // get rid of whitespace which will be at the front
fmt.Println(s)
}</lang>
 
{{out}}
<pre>
aaaeeefgggggilmmnnoooprrrruv
</pre>
 
=={{header|Ring}}==
9,490

edits