Reputation: 313
Firstly I have to admit I'am beginner in VB.net. I have this code that have to sort array in ascending order. Firstly it will request how much array size and then insert a data. But I face problem to make it working .Can anyone help me for this? Below is my code :
Module Module1
Sub Main()
Dim A(20) As Integer
Dim num, i, j, k, arr, temp As Integer
Console.Write("enter size num:")
Dim add = Console.ReadLine
If Integer.TryParse(add, num) Then
'Console.WriteLine("valid. num = " & num)
For i = 0 To num - 1
Console.Write("enter num:")
A(i) = Console.ReadLine
Next i
For i = 0 To num - 1
For j = i + 1 To num - j
If A(i) > A(j) Then
temp = A(i)
A(i) = A(j)
A(j) = temp
End If
Next j
Console.Write(A(i))
Next i
Else
Console.WriteLine("Invalid.Data is not number")
End If
Console.ReadLine()
End Sub
End Module
Thanks and any help will be greatly appreciate.
Upvotes: 0
Views: 780
Reputation: 22054
The immediate cause of your problem is that your
Console.Write(A(i))
is being invoked before you have completed the sorting operation. A secondary problem is that you are not validating the entered numbers, and you really ought to specify Option Strict On
at the top of the code and clean up the compilation errors that result.
If you are writing this as an exercise, that's fine but for production purposes you should prefer Tim Schmelter's method.
Upvotes: 2
Reputation: 460098
You haven't mentioned your problem. However, in general sorting an Int32()
is very easy, you can use Array.Sort
:
Array.Sort(A) ' finished '
Upvotes: 0