Jump to content

Ordenar numeros


bgmc
 Share

Recommended Posts

Por exemplo (usando colecções)

        Dim numList As New List(Of Integer)
        numList.Add(1)
        numList.Add(24)
        numList.Add(12)
        numList.Add(33)
        numList.Add(8)

        numList.Sort()

        For Each num As Integer In numList
            Debug.WriteLine(num)
        Next
Link to comment
Share on other sites

Ou se preferires um array:

        Dim numList(5) As Integer
        numList(0) = 12
        numList(1) = 1
        numList(2) = 24
        numList(3) = 3
        numList(4) = 8
        numList(5) = 2

        Array.Sort(numList)

        For Each num As Integer In numList
            Debug.WriteLine(num)
        Next
Link to comment
Share on other sites

Ou talvez até algo mais "manual", como costumam pedir nos exercicíos:

       Dim Numeros() = {6, 3, 1, 26, 13, 24, 8, 54, 12, 32, 65, 22, 7}

        Dim TempNum As Integer
        For i As Integer = Numeros.Length - 1 To 0 Step -1
            If i >= 1 Then
                If Numeros(i) < Numeros(i - 1) Then
                    TempNum = Numeros(i)
                    Numeros(i) = Numeros(i - 1)
                    Numeros(i - 1) = TempNum
                    i = Numeros.Length
                End If
            End If
        Next

Sérgio Ribeiro


"Great coders aren't born. They're compiled and released"
"Expert coders do not need a keyboard. They just throw magnets at the RAM chips"

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.