0

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.

4
  • " I face problem to make it working" what was the problem? However, it's simple as Array.Sort(A) Commented Nov 27, 2015 at 8:54
  • @TimSchmelter I still can't make it sorting in ascending order . When I run the code ,it will show the list of array but not in ascending order Commented Nov 27, 2015 at 8:56
  • Please show the new code containing Array.Sort(A) that still doesn't work. Commented Nov 27, 2015 at 9:14
  • When you run the code what result do you actually get? Commented Nov 27, 2015 at 10:50

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Ha! Yes I should have seen that.
0

You haven't mentioned your problem. However, in general sorting an Int32() is very easy, you can use Array.Sort:

Array.Sort(A) ' finished '

2 Comments

Is it possible I can sorting the same like my code given ?Btw thanks
@art: i have currently not the time to investigate your algorithm further. But in productiuon code use Array.Sort, it's much clearer, safer and more efficient.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.