Anonymous user
Sort the letters of string in alphabetical order: Difference between revisions
Sort the letters of string in alphabetical order (view source)
Revision as of 21:06, 25 July 2021
, 2 years ago→{{header|Go}}: use built-in sort algorithm
m (→{{header|Haskell}}: Used partition for the rough and ready sort) |
(→{{header|Go}}: use built-in sort algorithm) |
||
Line 82:
=={{header|Go}}==
<lang go>package main
import (
"fmt"
"sort"
"strings"
)
// type and methods satisfying sort.Interface
func bubbleSort(s string, trim bool) string { // allow optional removal of whitespace▼
type
func (x runeSlice) Len() int { return len(x) }
func (x runeSlice) Less(i, j int) bool { return x[i] < x[j] }
func (x runeSlice) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
▲func
s = string(chars)
if trim {
Line 122 ⟶ 113:
trims := []bool{true, false}
for i, s := range ss {
res :=
fmt.Printf("Unsorted->%s\n", s)
fmt.Printf("Sorted ->%s\n\n", res)
|