Arrays

Sorting

 

Sorting arrays is very simple in VB.NET. We just need to call the Array.Sort method and pass the array to be sorted. The method takes care of sorting according to the type of array (Integer, String, Date, etc.). The Array.Sort method sorts the data in the array in ascending sequence (low to high).  To sort an array in descending sequence (high to low), the Array.Reverse method can be used. The sample program below illustrates both these methods.

 

Sample Program 1 – Sorting Integer Array

 

VB Code:

Screen-shot of run:

Module Module1

 

    Private maintRandomNumbers(4) As Integer

 

    Sub Main()

 

        PopulateArray()

 

        Console.WriteLine("The numbers sorted are:")

        Array.Sort(maintRandomNumbers)

        For intX = 0 To 4

            Console.Write(CStr(maintRandomNumbers(intX)).PadLeft(5))

        Next

        Console.WriteLine()

        Console.WriteLine()

 

        Console.WriteLine("The numbers in descending sequence are:")

        Array.Reverse(maintRandomNumbers)

        For intX = 0 To 4

            Console.Write(CStr(maintRandomNumbers(intX)).PadLeft(5))

        Next

        Console.WriteLine()

        Console.WriteLine()

 

        Console.WriteLine("Press Enter to close this window.")

        Console.ReadLine()

 

    End Sub

 

    Private Sub PopulateArray()

 

        Dim intX As Integer

 

        Console.WriteLine("The numbers generated are:")

 

        Randomize()

        For intX = 0 To 4

            maintRandomNumbers(intX) = GetRandomNumber(100, 999)

            Console.Write(CStr(maintRandomNumbers(intX)).PadLeft(5))

        Next

 

        Console.WriteLine()

        Console.WriteLine()

 

    End Sub

 

   Private Function GetRandomNumber(ByVal pintLowerBound As Integer, _

                                     ByVal pintUpperBound As Integer) _

    As Integer

 

        ' This function will return a random integer that falls within

        ' the range of the two arguments passed.

 

        Return Int((pintUpperBound - pintLowerBound + 1) * Rnd() _

                  + pintLowerBound)

 

    End Function

 

End Module

 

Download the project files for Sample Program 1 here.

 

Sample Program 2 – Sorting String Array

 

Sample Program 2 applies the Array.Sort method an array of "records", where the "record" is simply a string containing fields from an input record of a sequential file as well as an extra area where we build a "sort key". The sample program reads in a file of unsorted customer data and can sort the records of that file in one of two ways. The first way is by customer last name alone; the second is by customer last name within state.

 

The program presents a menu where the user can choose how they want the file sorted. In the screen shot to the right, "1" is selected to sort by last name.

The data from the file is displayed, sorted by customer last name.

Back to the menu, the user enters "2" to sort the file by state and last name.

The data from the file is displayed, sorted by state, and last name within state (for each set of records where the state field is the same, the customers are listed alphabetically by last name).

 

 

 

Download the project files for Sample Program 2 here.