Re: Sorting names in Alphabetical order help for Golang noob

1,173 views
Skip to first unread message
Message has been deleted

Matt Silverlock

unread,
Dec 4, 2015, 2:14:34 AM12/4/15
to golang-nuts, culver...@gmail.com
Read this: https://blog.golang.org/slices :)



On Friday, December 4, 2015 at 2:21:36 PM UTC+8, culver...@gmail.com wrote:
Greetings,

I choose to learn and write a program in Go for a Programming class. I am having a bit of trouble sorting my strings by alphabetical order. Here is my code:

func main() {
    fmt.Println("Enter 10 Names")
    var names [10]string
    for i := 0; i < 10; i++ {
        in := bufio.NewReader(os.Stdin)
        line, _ := in.ReadString('\n')
        names[i] = line
    }
fmt.Println("Print the names in the order they were entered.")
    for j := 0; j < 10; j++ {
        fmt.Println(names[j])
    }
fmt.Println("Sort the names in alphabetical order, and print the sorted list.")
sort.Strings(names)
fmt.Println(names)

I get the following error from my compiler: cannot use names (type [10]String) as type []string in argument to sort.Strings.

Can someone point me in the right direction?

Thanks in advance!

Dave Cheney

unread,
Dec 4, 2015, 2:17:40 AM12/4/15
to golang-nuts
Names is an array, which is a distinct type. Almost everything in Go takes a slice, and for your example you probably want to declare names to be to.

var names = make([]string, 10) // a slice of strings with an initial length and capacity of 10

Message has been deleted

Matt Harden

unread,
Dec 4, 2015, 11:57:29 PM12/4/15
to culver...@gmail.com, golang-nuts
Think about the type of your names variable, and the type of the sort.Strings function. Also your SortString function probably doesn't do what you think it does.

On Fri, Dec 4, 2015 at 5:16 PM <culver...@gmail.com> wrote:
I have adjusted my code to reject the following:

import (
    "bufio"
    "fmt"
    "os"
"sort"
    "strings"
)

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

func main() {
    fmt.Println("Enter 10 Names")
// a slice of strings with an initial length and capacity of 10
var names = make([]string, 10)
    for i := 0; i < 10; i++ {
        in := bufio.NewReader(os.Stdin)
        line, _ := in.ReadString('\n')
        names[i] = line
    }
fmt.Println("\n")
fmt.Println("Print the names in the order they were entered.")
fmt.Println("\n")
    for j := 0; j < 10; j++ {
        fmt.Println(names[j])
    }
fmt.Println("Sort the names in alphabetical order, and print the sorted list.")
fmt.Println("\n")
 
x := SortString(names)
fmt.Println(x)
fmt.Println("\n")
fmt.Println("Reverse the order of your names, and print them in that order.")
fmt.Println("\n")
    for l := 10; l > 0; l-- {
        fmt.Println(names[l-1])
    }
}


*****When trying to run the program, I receive the following error:

cannot use names (type []string) as type string in argument to SortString.

I have attempted to figure out a solution, but I am lost........Any Suggestions?

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Message has been deleted

Tamás Gulácsi

unread,
Dec 5, 2015, 2:48:36 AM12/5/15
to golang-nuts
sort.Strings, and not SortString.
Reply all
Reply to author
Forward
0 new messages