arraysvb.netsorting

Array.Sort is not working


I am very new at programming and learning it via Visual Basic for Beginners by wrox.com. So I am learning Arrays. In the practical part of the book there is a method for sorting arrays. I am doing the same as it was instructed in the books. But programm says the Sort is not a member of Array (Which means Array.Sort is incorrect). But I took a look at several examples in internet where Array.Sort is working for them.

enter image description here


Solution

  • As I saw in image, StrFriends is a LIST not an ARRAY. Array.Sort is valid for arrays only, not to Lists. If you need to sort a list, you need to convert it to array, like this:

                Dim cave As List(Of String) = New List(Of String)
                Array.Sort(cave.ToArray)
    

    or just operate as ARRAY:

                dim cave as String() = {"j", "c", "a", "b"}
                Array.Sort(cave)
    

    or

                dim cave(0 to 3) as String
                cave(0) = "j"
                cave(1) = "c"
                cave(2) = "a"
                cave(3) = "b"
                Array.Sort(cave)