I am trying to make a basic console application that allows the user to input 3 names, and then sort it alphabetically. This is not working.
Here is my code.
Dim names(2) As String
Console.WriteLine("Name 1 ?")
names(0) = Console.ReadLine
Console.WriteLine("Name 2 ?")
names(1) = Console.ReadLine
Console.WriteLine("Name 3 ?")
names(2) = Console.ReadLine
Array.Sort(names)
Console.WriteLine("Your names are:" & names)
The console is not printing the code.
Using LINQ:
Dim namesSorted() As String = names.OrderBy(Function(x) x).ToArray
With this approach you can change sort criteria to anything you want, ie word length, ascending/descending. To print the results:
Console.WriteLine("Your names are:" & String.Join(","c, namesSorted))
Also, I suggest you use List instead, then you are not limited to just 3 names, and you don't need to know how many names you will be processing in advance. LINQ syntax will be the same.
Try something like this...
Dim names(2) As String
Console.WriteLine("Name 1 ?")
names(0) = Console.ReadLine
Console.WriteLine("Name 2 ?")
names(1) = Console.ReadLine
Console.WriteLine("Name 3 ?")
names(2) = Console.ReadLine
Array.Sort(names)
Console.WriteLine("Your names are:")
For x = 0 To 2
Console.WriteLine(names(x))
Next x
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.