GoLang: Command Line Tool to Sort Strings
We can sort a list of given strings from stdin and output to stdout using the following GoLang:
package main
import (
"fmt"
"bufio"
"os"
"sort"
"strings"
)
func main() {
reader := bufio.NewReader(os.Stdin)
var data = []string{}
for true {
s, _ := reader.ReadString('\n')
s = strings.Trim(s, "\n")
if len(s) == 0 {
break
}
data = append(data, s)
}
sort.Strings(data)
for _, a := range data {
fmt.Println(a)
}
}
The list of string data is constructed from os.Stdin line by line. And we call sort.Strings to sort them alphabetically in ascending order. Then we iterate over the sorted list and print them out to standard output.
For example:
$ go run sort.go
aaa
ccc
bbb
Ctrl + D
aaa
bbb
ccc
Reposted to Blog
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Thank you for reading ^^^^^^^^^^^^^^^
NEW! Following my Trail (Upvote or/and Downvote)
Follow me for topics of Algorithms, Blockchain and Cloud.
I am @justyy - a Steem Witness
https://steemyy.com
My contributions
- Video Downloader
- Steem Blockchain Tools
- Free Cryptos API
- VPS Database
- Computing Technology Blog
- A few useless tools
- And some other online software/tools
- Merge Files/Videos
- LOGO Turtle Programming Chrome Extension
- Teaching Kids Programming - Youtube Channel and All Contents
Delegation Service
Important Update of Delegation Service!
Support me
If you like my work, please:
- Buy Me a Coffee, Thanks!
- Become my Sponsor, Thanks!
- Voting for me:
https://steemit.com/~witnesses type in justyy and click VOTE
- Delegate SP: https://steemyy.com/sp-delegate-form/?delegatee=justyy
- Vote @justyy as Witness: https://steemyy.com/witness-voting/?witness=justyy&action=approve
- Set @justyy as Proxy: https://steemyy.com/witness-voting/?witness=justyy&action=proxy
Alternatively, you can vote witness or set proxy here: https://steemit.com/~witnesses
ezpz lexical sort
Hello Sir
It was very Amazing ..